Answers for "how to put unique object value to arrya in js"

13

unique objects in array javascript

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.
Posted by: Guest on September-14-2020
0

javascript add unique values to array

const arr = Array.from({length: 5}, v => Math.floor(Math.random() * 100));
console.log(arr) // Array with random numbers of length 5
Posted by: Guest on January-19-2022

Code answers related to "how to put unique object value to arrya in js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language