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 :: js for each character in string 
Javascript :: vue-cli-service 
Javascript :: javascript date minus seconds 
Javascript :: disable scroll jquery 
Javascript :: node express server static files 
Javascript :: token invalid discord bot 
Javascript :: python convert requests response to json 
Javascript :: innerwidth react 
Javascript :: jquery checkbox checked value 
Javascript :: jquery each break 
Javascript :: javascript remove non numeric chars from string keep dot 
Javascript :: js get custom attribute 
Javascript :: change attribute 
Javascript :: update node js ubuntu 
Javascript :: @types react-router-dom 
Javascript :: react index.js 
Javascript :: ansi encoding "vscode" 
Javascript :: moment add 30 days 
Javascript :: react native italic text 
Javascript :: gradlew clean in react native 
Javascript :: js onsubmit prevent default 
Javascript :: write file with deno 
Javascript :: how to find prime numbers in an array in javascript 
Javascript :: javascript detect mobile device 
Javascript :: javascript get filename from url 
Javascript :: javascript count elements in json object 
Javascript :: how to insalk react router with npm 
Javascript :: javascript wait 1 second 
Javascript :: how to get file name in directory node js 
Javascript :: javascript canvas touchmove 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =