is there a clever way to get all objects with same value?
[
{ name: 'a' , value: '123'},
{ name: 'b' , value: '123'},
{ name: 'c' , value: '1234'},
{ name: 'd' , value: '1234'},
{ name: 'e' , value: '12345'},
{ name: 'f' , value: '123456'}
]
the ideal result is:
[
{ name: 'a' , value: '123'},
{ name: 'b' , value: '123'},
{ name: 'c' , value: '1234'},
{ name: 'd' , value: '1234'}
]
I am thinking to count all the occurance of values, but that seems not a good way.
Appreciate!
You could take a Map
and filter the array by the value of the map.
var array = [{ name: 'a' , value: '123' }, { name: 'b' , value: '123' }, { name: 'c' , value: '1234' }, { name: 'd' , value: '1234' }, { name: 'e' , value: '12345' }, { name: 'f' , value: '123456' }],
counts = array.reduce((m, { value }) => m.set(value, m.has(value)), new Map),
result = array.filter(({ value }) => counts.get(value));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }