D
D
dmitry20002021-06-18 22:11:10
JavaScript
dmitry2000, 2021-06-18 22:11:10

How to uniqueize an array of objects?

An array of strings by type can be uniqueized using A if, for example, an array of objectslet arr = ['Один', 'Два', 'Два', 'Три']
arr = Array.from(new Set(arr));

let arr = [
                { label: 'Критичная', value: 'critical' },
                { label: 'Средняя', value: 'medium' },
                { label: 'Не критичная', value: 'low' },
                { label: 'Не критичная', value: 'low' },
                { label: 'Не критичная', value: 'low' }
            ];

Is it possible to do the same with it, remove unnecessary objects by value or key?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-06-18
@dmitry2000

[...new Map(arr.map(n => [ n.value, n ])).values()]

or
Object.values(arr.reduce((acc, n) => (acc[n.value] = n, acc), {}))

or
arr.filter((n, i, a) => n === a.find(m => m.value === n.value))

or
Array.from(new Set(arr.map(n => n.value)), n => arr.find(m => m.value === n))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question