score:1

Accepted answer

Under the assumption that your data is so well groomed, a quick and dirty way is to combine the object's values into strings and make a Set out of them. Sets, by nature, cannot have duplicates. But at the same time, JavaScript Sets can only understand duplicates that are primitive data types (numbers, strings, etc.)

Concat the values, turn it into a Set, turn it back into the data structure it was previously.

Please tell me if you find anything confusing about my syntax. I make liberal use of modern JavaScript syntax.

Edit: You can use Array#filter to be selective as to what should exist in the array.

const values = [
    {
        "name": "this_year",
        "node": "NA"
    },
    {
        "name": "this_year",
        "node": "1"
    },
    {
        "name": "this_year",
        "node": "4"
    },
    {
        "name": "this_year",
        "node": "2"
    },
    {
        "name": "this_year",
        "node": "4"
    },
    {
        "name": "this_year",
        "node": "1"
    }
]
function constructUniques(array) {
    const concattedStringValues = array
           .filter(({node}) => node !== 'NA')
           .map(({name, node}) => `${name} ${node}`)
    const uniqueStrings = new Set(concattedStringValues)
    return [...uniqueStrings].map(concattedVal => {
        const [name, node] = concattedVal.split(' ')
        return {name, node }
    })
}

console.log(constructUniques(values))


Related Query

More Query from same tag