Answers for "js extract all unique values from array of objects"

15

javascript get unique values from array

const myArray = [1,2,3,1,5,8,1,2,9,4];
const unique = [...new Set(myArray)]; // [1, 2, 3, 5, 8, 9, 4]

const myString = ["a","b","c","a","d","b"];
const uniqueString = [...new Set(myString)]; //["a", "b", "c", "d"]
Posted by: Guest on July-25-2020
1

javascript unique array of objects by property

const array =
  [
    { "name": "Joe", "age": 17 },
    { "name": "Bob", "age": 17 },
    { "name": "Carl", "age": 35 }
  ]

function uniqueByKey(array, key) {
  return [...new Map(array.map((x) => [x[key], x])).values()];
}

console.log(uniqueByKey(array, 'age'));
Posted by: Guest on November-15-2020

Code answers related to "js extract all unique values from array of objects"

Code answers related to "Javascript"

Browse Popular Code Answers by Language