Removing items from an array of objects based on duplicate values โ€‹โ€‹of multiple keys

I have an array of such objects -

var arr = [ { type_id: "3", full_empty:"true", quantity:1}, { type_id: "3", full_empty:"true", quantity:1}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"false", quantity:4}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"true", quantity:4} ]; 

I want to remove duplicates with the same type_id and full_empty values . The result should look like this:

 var arr = [ { type_id: "3", full_empty:"true", quantity:1}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"false", quantity:4}, ]; 

I searched and found some solution, but some of them are designed to remove duplicate keys or to remove duplicates based on the duplicate value of only one key. Some required external libraries. There are also some solutions that I cannot understand. Is there an easy way to do this in simple JavaScript?

Edit for a better understanding . I read this question . The accepted answer to this question is to find duplication for only one key. In my case, I have to find duplication for several keys.

+7
javascript filter
source share
7 answers

You can use pure functions using Array.some () and Array.reduce () to reduce your input array to an array of individual elements, as shown below.

  var arr = [ { type_id: "3", full_empty:"true", quantity:1}, { type_id: "3", full_empty:"true", quantity:1}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"false", quantity:4}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"true", quantity:4} ]; var a = arr.reduce(function (accumulator, current) { if (checkIfAlreadyExist(current)) { return accumulator } else { return accumulator.concat([current]); } function checkIfAlreadyExist(currentVal) { return accumulator.some(function(item){ return (item.type_id === currentVal.type_id && item.full_empty === currentVal.full_empty); }); } }, []); console.log(a); 

Short ES6 Syntax

A shorter reduce can be written using the ES6 arrow functions and the distribution operator, as shown below:

 var arr = [ { type_id: "3", full_empty:"true", quantity:1}, { type_id: "3", full_empty:"true", quantity:1}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"false", quantity:4}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"true", quantity:4}, { type_id: "9", full_empty:"true", quantity:4} ]; var a = arr.reduce((accumulator, current) => { if (checkIfAlreadyExist(current)) { return accumulator; } else { return [...accumulator, current]; } function checkIfAlreadyExist(currentVal) { return accumulator.some((item) => { return (item.type_id === currentVal.type_id && item.full_empty === currentVal.full_empty); }); } }, []); console.log(a); 
+6
source share

Despite other solutions, I suggest using a hash table with the type_id key and full_empty as the key, and if a new one is found, set hash to true. Together with Array#filter you get a new array with unique elements.

 var arr = [{ type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "false", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }], filtered = arr.filter(function (a) { var key = a.type_id + '|' + a.full_empty; if (!this[key]) { this[key] = true; return true; } }, Object.create(null)); console.log(filtered); 

ES6

 var arr = [{ type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "false", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }], filtered = arr.filter( (temp => a => (k => !temp[k] && (temp[k] = true))(a.type_id + '|' + a.full_empty) )(Object.create(null)) ); console.log(filtered); 
+2
source share
 //To search the element is already exisit or not.(to remove Duplicate) function searchExisting(type_id,full_empty,newArray){ for(var i=0;i<newArray.length;i++){ if(newArray[i].type_id==type_id && newArray[i].full_empty==full_empty){ return true; } } return false; } //loop through every element and push it into new array var arr2=[]; for(var i=0;i<arr.length;i++){ if(!searchExisting(arr[i].type_id,arr[i].full_empty,arr2)){ arr2.push(arr[i]); } } console.log(arr2) 
+1
source share

You can use find and forEach to create a new array from this array that contains duplicate values

We hope this piece will be useful

 var arr = ["Json Array object as supplied in the question"]; // A new array which will contain unique json object var newArray = []; //Loop through each of the object in the original array arr.forEach(function(item) { // If newArray .length is zero then just push the first element // else in newArray find if a json object already exist which have same // type_id & full_empty. If it does not exist it will return undefined if (newArray.length !== 0) { var _isPresent = newArray.find(function(secItem) { return secItem.type_id === item.type_id && secItem.full_empty === item.full_empty }) // If element is not present then push this json pbject if (_isPresent == undefined) { newArray.push(item) } } else { // this will execute only once when newArray length is 0 newArray.push(item) } }) console.log(newArray) 

JSFIDDLE

+1
source share

It's not as scary as Nina's answer , but a new answer can be seen.

 var arr = [ { type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "3", full_empty: "true", quantity: 1 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "false", quantity: 4}, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4 }, { type_id: "9", full_empty: "true", quantity: 4}]; var dict = {}, result = []; arr.forEach((i, key) => { !dict[(key = i.type_id + i.full_empty)] && (dict[key] = result.push(i)); }) console.log(result) 
+1
source share

The array object has a filter and a map. You can use the filter to adjust the properties of the inactive one that you want. This is a logical evaluation that returns a โ€œfilteredโ€ new array.

Here's a great video posted on Egghead.IO that really violates it.

0
source share

If you do not want to get into the code to use the snippet below: -

 var sDat = [ { sid:12, scode:"code", sname:"Deep" }, { sid:12, scode:"code", sname:"Anand" }, { sid:139, scode:"code", sname:"Singh"} ]; function cleanup(arr, prop) { var new_arr = []; var lookup = {}; for (var i in arr) { lookup[arr[i][prop]] = arr[i]; } for (i in lookup) { new_arr.push(lookup[i]); } return new_arr; } var n = cleanup(sDat, 'sid'); alert(n); 

I hope this works for you.

0
source share

All Articles