Answers for "find unique objects in an array of objects in javascript 5"

8

unique objects in array javascript

const array =
  [
    { "name": "Joe", "age": 17 },
    { "name": "Bob", "age": 17 },
    { "name": "Carl", "age": 35 }
  ]

const key = 'age';

const arrayUniqueByKey = [...new Map(array.map(item =>
  [item[key], item])).values()];

console.log(arrayUniqueByKey);

   /*OUTPUT
       [
        { "name": "Bob", "age": 17 },
        { "name": "Carl", "age": 35 }
       ]
   */

 // Note: this will pick the last duplicated item in the list.
Posted by: Guest on September-14-2020
0

javascript find unique values in array of objects

var flags = [], output = [], l = array.length, i;
for( i=0; i<l; i++) {
    if( flags[array[i].age]) continue;
    flags[array[i].age] = true;
    output.push(array[i].age);
}
Posted by: Guest on January-07-2021
0

find unique objects in an array of objects in javascript 5

var array = [{"name":"Joe", "age":17}, {"name":"Bob", "age":17}, {"name":"Carl", "age": 35}];
var unique = [];
var distinct = [];
for(var i = 0; i < array.length; i++ ){
  if(!unique[array[i].age]){
    distinct.push(array[i]);
    unique[array[i].age] = 1;
  }
}


gs.info(distinct);
Posted by: Guest on October-27-2021

Code answers related to "find unique objects in an array of objects in javascript 5"

Code answers related to "Javascript"

Browse Popular Code Answers by Language