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 :: if js 
Javascript :: how to check if local storage is available 
Javascript :: HashRouter 
Javascript :: use moment js in ejs file 
Javascript :: send data from servlet to hjsp 
Javascript :: javascript Create Strings 
Javascript :: p5.js typescript 
Javascript :: includes in js 
Javascript :: Select HTML elements by CSS selectors 
Javascript :: best reactjs course on udemy 
Javascript :: css select all links in div 
Javascript :: find items in array not in another array javascript 
Javascript :: replace characters form array 
Javascript :: tricky javascript interview questions 
Javascript :: fade in onscroll jquery 
Javascript :: enable swipe using javascript 
Javascript :: jquery ajax methods 
Javascript :: how to subtract time in javascript 
Javascript :: split whitespace except in quotes javascript 
Javascript :: jquery get value of element 
Javascript :: javascript input value change 
Javascript :: how to convert div to image in jquery 
Javascript :: How to update one mongoose db 
Javascript :: date and time javascript 
Javascript :: button disable in js 
Javascript :: numero aleatorio javascript 
Javascript :: sequelize transaction config 
Javascript :: upload file react onclick 
Javascript :: react native conditional rendering 
Javascript :: js detect object has key 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =