根据 JavaScript 中的属性值从对象数组中删除重复项
Remove duplicate from array of objects based on value of properties in JavaScript
如何根据 name
属性 从数组 someArray
中删除重复项,条件是如果 name
对于两个元素相同但是对于其中一个 type
是 new
,原始的(没有类型 new)将被保留?
someArray = [{id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 3, name:"apple", type: "new"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]
到
[{id: 1, name:"apple"}, {id: 2, name: "mango"}, {id: 4, name:"orange"}, {id: 6, name: "pineapple", type: "new"}]
可以使用Array.prototype.reduce筛选出满足条件的项目
const
input = [
{ id: 1, name: "apple" },
{ id: 2, name: "mango" },
{ id: 3, name: "apple", type: "new" },
{ id: 4, name: "orange" },
{ id: 5, name: "orange", type: "new" },
{ id: 6, name: "pineapple", type: "new" },
],
output = Object.values(
input.reduce((r, o) => {
if (!r[o.name] || (r[o.name].type === "new" && o.type !== "new")) {
r[o.name] = o;
}
return r;
}, {})
);
console.log(output);
您也可以使用 Spread Syntax。
const
input = [
{ id: 1, name: "apple" },
{ id: 2, name: "mango" },
{ id: 3, name: "apple", type: "new" },
{ id: 4, name: "orange" },
{ id: 5, name: "orange", type: "new" },
{ id: 6, name: "pineapple", type: "new" },
],
output = Object.values(
input.reduce(
(r, o) =>
!r[o.name] || (r[o.name].type === "new" && o.type !== "new")
? { ...r, [o.name]: o }
: r,
{}
)
);
console.log(output);
您可以迭代数组并评估是否 'type: new' 并且尚不存在,然后弹出该项目。
let someArray = [{
id: 1,
name: "apple"
}, {
id: 2,
name: "mango"
}, {
id: 3,
name: "apple",
type: "new"
}, {
id: 4,
name: "orange"
}, {
id: 5,
name: "orange",
type: "new"
}, {
id: 6,
name: "pineapple",
type: "new"
}];
console.info('Initial value:' + JSON.stringify(someArray));
someArray.forEach(function(item, index, object) {
if (item.type === 'new' && someArray.filter(val => val.name === item.name && !val.type)) {
object.splice(index, 1);
}
});
console.info('Desired Result:' + JSON.stringify(someArray));
let someArray = [{id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 3, name:"apple", type: "new"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]
someArray.sort(function (a, b) {
if (a.type !== 'undefined') return -1
return 0
})
const result = someArray.reduce((resArr, currentArr) => {
let other = resArr.some((ele) => currentArr.name === ele.name)
if (!other) resArr.push(currentArr)
return resArr
}, [])
console.log(result)
您可以使用 Map 按名称组合值,如果有两个值同名,只需使用不带 type = "new"
的值
let someArray = [{id: 3, name:"apple", type: "new"}, {id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]
function getUnique(arr){
let mapObj = new Map()
arr.forEach(v => {
let prevValue = mapObj.get(v.name)
if(!prevValue || prevValue.type === "new"){
mapObj.set(v.name, v)
}
})
return [...mapObj.values()]
}
console.log(getUnique(someArray))
如何根据 name
属性 从数组 someArray
中删除重复项,条件是如果 name
对于两个元素相同但是对于其中一个 type
是 new
,原始的(没有类型 new)将被保留?
someArray = [{id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 3, name:"apple", type: "new"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]
到
[{id: 1, name:"apple"}, {id: 2, name: "mango"}, {id: 4, name:"orange"}, {id: 6, name: "pineapple", type: "new"}]
可以使用Array.prototype.reduce筛选出满足条件的项目
const
input = [
{ id: 1, name: "apple" },
{ id: 2, name: "mango" },
{ id: 3, name: "apple", type: "new" },
{ id: 4, name: "orange" },
{ id: 5, name: "orange", type: "new" },
{ id: 6, name: "pineapple", type: "new" },
],
output = Object.values(
input.reduce((r, o) => {
if (!r[o.name] || (r[o.name].type === "new" && o.type !== "new")) {
r[o.name] = o;
}
return r;
}, {})
);
console.log(output);
您也可以使用 Spread Syntax。
const
input = [
{ id: 1, name: "apple" },
{ id: 2, name: "mango" },
{ id: 3, name: "apple", type: "new" },
{ id: 4, name: "orange" },
{ id: 5, name: "orange", type: "new" },
{ id: 6, name: "pineapple", type: "new" },
],
output = Object.values(
input.reduce(
(r, o) =>
!r[o.name] || (r[o.name].type === "new" && o.type !== "new")
? { ...r, [o.name]: o }
: r,
{}
)
);
console.log(output);
您可以迭代数组并评估是否 'type: new' 并且尚不存在,然后弹出该项目。
let someArray = [{
id: 1,
name: "apple"
}, {
id: 2,
name: "mango"
}, {
id: 3,
name: "apple",
type: "new"
}, {
id: 4,
name: "orange"
}, {
id: 5,
name: "orange",
type: "new"
}, {
id: 6,
name: "pineapple",
type: "new"
}];
console.info('Initial value:' + JSON.stringify(someArray));
someArray.forEach(function(item, index, object) {
if (item.type === 'new' && someArray.filter(val => val.name === item.name && !val.type)) {
object.splice(index, 1);
}
});
console.info('Desired Result:' + JSON.stringify(someArray));
let someArray = [{id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 3, name:"apple", type: "new"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]
someArray.sort(function (a, b) {
if (a.type !== 'undefined') return -1
return 0
})
const result = someArray.reduce((resArr, currentArr) => {
let other = resArr.some((ele) => currentArr.name === ele.name)
if (!other) resArr.push(currentArr)
return resArr
}, [])
console.log(result)
您可以使用 Map 按名称组合值,如果有两个值同名,只需使用不带 type = "new"
let someArray = [{id: 3, name:"apple", type: "new"}, {id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]
function getUnique(arr){
let mapObj = new Map()
arr.forEach(v => {
let prevValue = mapObj.get(v.name)
if(!prevValue || prevValue.type === "new"){
mapObj.set(v.name, v)
}
})
return [...mapObj.values()]
}
console.log(getUnique(someArray))