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 get user location 
Javascript :: Check if a JavaScript string is a URL 
Javascript :: how to make a screen recording software with js 
Javascript :: jquery validation on button click 
Javascript :: react font awesome npm 
Javascript :: convert dict to json python 
Javascript :: angular readonly if value is not null 
Javascript :: click a link using jquery 
Javascript :: brand icons in next js 
Javascript :: check if element has childs jquery 
Javascript :: javascript remove last character 
Javascript :: javascript through array 
Javascript :: node cron every second 
Javascript :: capturar el valor de un input con jquery 
Javascript :: javascript store in localstorage 
Javascript :: put two buttons in a row in react native 
Javascript :: javascript trim each element in array 
Javascript :: copy text to clipboard javascript react 
Javascript :: rails routes default format json 
Javascript :: javascript assign value to input using name 
Javascript :: javascript loop through all element children 
Javascript :: jquery on blur 
Javascript :: &nbsp replace javascript 
Javascript :: destroy chart js 
Javascript :: This version of CLI is only compatible with Angular versions 0.0.0 || ^10.0.0-beta || =10.0.0 <11.0.0, but Angular version 9.1.3 was found instead. 
Javascript :: how to detect a button click in javascript 
Javascript :: fetch data from api url 
Javascript :: truncate function react 
Javascript :: LazyLoad for images, Videos and Iframes JavaScript and JQuery 
Javascript :: mongoose unique 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =