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 :: react router Link does work 
Javascript :: react scroll to top 
Javascript :: javascript randomly shuffle array 
Javascript :: get alert after using ajax 
Javascript :: call a function whenever routerlink is clicke angular 
Javascript :: pdf dark 
Javascript :: jquery first child 
Javascript :: javascript get users location 
Javascript :: valid phone number regex with country code 
Javascript :: express messages 
Javascript :: add a trailing slash javascript url 
Javascript :: js upload file dialog 
Javascript :: matrix elements sum javascript 
Javascript :: Jquery get value of dropdown selected 
Javascript :: how to get day from javascript date 
Javascript :: nodejs increase heap size 
Javascript :: js computed style 
Javascript :: useref object is possibly null 
Javascript :: check either define or undefined in javascript 
Javascript :: stack implementation in javascript using array 
Javascript :: datatable visable show entries 
Javascript :: Get full year from date object 
Javascript :: how to truncate string in javascript 
Javascript :: get image url in react input file or preview form image 
Javascript :: uuid javascript 
Javascript :: jsonobject gradle dependency 
Javascript :: array sort by alphabetical javascript 
Javascript :: js navigate to another page 
Javascript :: javascript countdown 10 seconds 
Javascript :: how to start react project 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =