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 :: javascript check if two date are ugual 
Javascript :: discord.js create unexipable invite 
Javascript :: selectpicker append option 
Javascript :: Generate random whole numbers javascript 
Javascript :: javascript open link with button 
Javascript :: reactjs variable in string 
Javascript :: why is the radiators in cars painted black 
Javascript :: redirecting in react 
Javascript :: react link underline 
Javascript :: detect keypress 
Javascript :: jquery varable exists 
Javascript :: Datatables Text Align Center 1 Or More Column 
Javascript :: date constructor javascript 
Javascript :: vh not working on phone 
Javascript :: enable native bracket matching vs cide 
Javascript :: window onload javascript 
Javascript :: javascript remove last character from string 
Javascript :: fs in angular 
Javascript :: avaScript slice() With Negative index 
Javascript :: jquery get data-id 
Javascript :: javascript make beep sound 
Javascript :: express.json vs bodyparser.json 
Javascript :: vue prop array default 
Javascript :: How to remove text from a string in javscript 
Javascript :: google maps infowindow on hover 
Javascript :: missing from-clause entry for table sequelize limit 
Javascript :: detox scroll to element 
Javascript :: express get jwt token from header 
Javascript :: js map iterate 
Javascript :: update to angular 12 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =