JS Unique values

How to find unique values using javascipt

Initially let's have one array containing with objects. To find unique values in an given array we need to use new Set() method.

... this is called spread operator which is used in the code to display like

// With the spread operator
['engineer','software']
//without the ... spread operator we are getting the output like in the form of object.
[set{'engineer','software'} ]
// to find unique values we are using new Set() Method that has described below
const names=[
    {
        name:"John",
        job:"software"
    },
    {
        name:"Murray",
        job:"software"
    },
    {
        name:"John",
        job:"software"
    },
    {
        name:"Ravi",
        job:"engineer"
    }
]

const allNames= [...new Set (names.map((value)=>value.job))]
console.log(allNames); // ['software','engineer']

// if we want to add extra name before the given array

const result =  ["Andrew",...new Set (names.map((value)=>value.name))]
console.log(result); //[ 'Andrew', 'John', 'Murray', 'Ravi' ]

If you like article , share , review and add comments.