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

PREVIOUS NEXT
Code Example
Javascript :: how to pass callback function in javascript 
Javascript :: TypeError: this.setState is not a function 
Javascript :: sort object properties by value javascript 
Javascript :: convert json data to a html table 
Javascript :: setimout 
Javascript :: onomonrieah 
Javascript :: convert a signed 64.64 bit fixed point number online 
Javascript :: The element.onclick Property 
Javascript :: create multiple array buttons in javascript 
Javascript :: round down js 
Javascript :: use cors 
Javascript :: javascript traversing 
Javascript :: how to do when enter is pressed javascript do smething 
Javascript :: localhost:3000 ad is not working with outlook angular 8 
Javascript :: how to implement certain actions after setstate in react hooks 
Javascript :: node 14: fsevents@1.2.13: fsevents 1 will break on node 
Javascript :: nextjs link image 
Javascript :: javascript foreach table 
Javascript :: Material-ui wallet icon 
Javascript :: javascript get name from steamid 
Javascript :: Replacing String Content 
Javascript :: return jsx in for loop 
Javascript :: How to use `setState` callback on react hooks 
Javascript :: como colocar dados no firebase 
Javascript :: js user add names to a list on screen 
Javascript :: delay / sleep program in js 
Javascript :: using while loop to create table rows js 
Javascript :: calculate 7 days in javascript countdown 
Javascript :: javascript typeof array 
Javascript :: javascript closest data attribute 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =