Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

unique values from array of objects

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.
 Run code snippet
Comment

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);
Comment

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);
}
Comment

create unique set of array of objects

function unique(array, propertyName) {
   return array.filter((e, i) => array.findIndex(a => a[propertyName] === e[propertyName]) === i);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: fetch with bearer token 
Javascript :: dont drag img 
Javascript :: how to fix cors in angular 
Javascript :: replace string shopify 
Javascript :: if radio checked jquery 
Javascript :: jquery check if attribute exists 
Javascript :: vue js footer copyright date automatically 
Javascript :: the engine node is incompatible with this module 
Javascript :: data-dismiss= modal in jquery 
Javascript :: console.dir depth 
Javascript :: node list files in directory 
Javascript :: p5.js boilerplate 
Javascript :: console load jquery 
Javascript :: javascript get number from input 
Javascript :: lodash combine permissions 
Javascript :: js check file exist 
Javascript :: justifycontent react native flatlist 
Javascript :: how to select data attribute in javascript 
Javascript :: websocket sample code js 
Javascript :: get scroll position jquery 
Javascript :: javascript emit sound 
Javascript :: scrollview refresh react native 
Javascript :: import paper material ui 
Javascript :: javascript loop through string 
Javascript :: loopback geopoint 
Javascript :: js for of array with index 
Javascript :: how replace 0 without replace 10 in js 
Javascript :: disable copy past jquery 
Javascript :: sort object by value javascript 
Javascript :: update angular cli 10 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =