的方法有多种,以下是其中几种常见的方法:
const arr = [{id: 1}, {id: 2}, {id: 1}];
const uniqueValues = Array.from(new Set(arr.map(obj => obj.id)));
console.log(uniqueValues); // [1, 2]
const arr = [{id: 1}, {id: 2}, {id: 1}];
const uniqueValues = arr.reduce((acc, obj) => {
if (!acc.includes(obj.id)) {
acc.push(obj.id);
}
return acc;
}, []);
console.log(uniqueValues); // [1, 2]
const arr = [{id: 1}, {id: 2}, {id: 1}];
const uniqueValues = arr.filter((obj, index, self) => {
return self.findIndex(o => o.id === obj.id) === index;
}).map(obj => obj.id);
console.log(uniqueValues); // [1, 2]
以上是几种常见的从对象数组中获取唯一值的方法,根据具体的需求和场景选择合适的方法即可。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云