javascript unique array of objects by property
//ES6
let array = [
{ "name": "Joe", "age": 17 },
{ "name": "Bob", "age": 17 },
{ "name": "Carl", "age": 35 }
];
array.map(item => item.age)
.filter((value, index, self) => self.indexOf(value) === index)
> [17, 35]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'));const unique = [...new Set(array.map(item => item.age))];var flags = [], output = [], l = array.length, i;
for( i=0; i<l; i++) {
if( flags[array[i].age]) continue;
flags[array[i].age] = true;
output.push(array[i].age);
}
Also in JavaScript:
- convert app function to class component react native
- is undefined javascript
- scroll div horizontally with move wheel js
- how to remove last element in js
- jsonb_set remove key
- jquery cdm
- How to remove the first and the last character of a string
- watchman watch-del-all, and react-native start --reset-cache
- javascript add color effect
- next js typescript
- closure javascript mdn
- how to read if a person has send a message on discord.js
- const { message }
- jquery slideup
- set value of input javascript
- Write a javascript function that takes two arguments and should either sum them or just print the first one if the second one is undefined. If both are undefined print "No numbers given"
- js num to string with leading 0
- Arranging Coins
- js string contains
- js tab in textarea
- replace array element javascript stack overflow
- jquery on element change
- javascript e.keycode deprecated
- class inheritance method javascript