如何在 javascript 中检索具有相同 属性 值的对象数组
How to retrieve the array of objects having same property value in javascript
我有一个对象数组,如果 属性 items
具有相同的值,return javascript
中的那个数组对象
在下面的列表中,检索在
中具有相同值的对象数组
items
属性 在 javascript
注意:也是动态对象数组,项目值可能会有所不同,所以不能直接用值过滤
var list =[
{id: 1, name: "dev", items: "sales", code: "IN"},
{id: 2, name: "lena", items: "finance", code: "SG"}
{id: 3, name: "lisa", items: "sales", code: "AU"},
{id: 4, name: "mano", items: "marketing", code: "IN"}
]
Expected Result
{id: 1, name: "dev", items: "sales", code: "IN"}
{id: 3, name: "lisa", items: "sales", code: "AU"},
var result= list.reduce((m, o) => {
const found= m.find(e => e.items === o.items);
if(found){
m.push(o);
return m
}
}, []);
使用 reduce 找出所有重复项会更麻烦。
你可以使用闭包。
const list = [
{ id: 1, name: "dev", items: "sales", code: "IN" },
{ id: 2, name: "lena", items: "finance", code: "SG" },
{ id: 3, name: "lisa", items: "sales", code: "AU" },
{ id: 4, name: "mano", items: "marketing", code: "IN" },
{ id: 5, name: "lisa", items: "gul gul", code: "AU" },
{ id: 6, name: "anthony", items: "some", code: "AU" },
{ id: 7, name: "mark", items: "gul gul", code: "AU" }
];
const findByItems = (eq) => (arr) => arr.filter(
(x, i) => arr.find((y, j) => i !== j && eq(x, y))
)
const duplicatedItems = findByItems((a, b) => a.items === b.items);
console.log(duplicatedItems(list))
我个人会根据 items 值对输入中的项目进行分组,然后提取最长的一组(在您的示例中,财务和营销将有一个项目只有销售会有两个):
var list = [
{id: 1, name: "dev", items: "sales", code: "IN"},
{id: 2, name: "lena", items: "finance", code: "SG"},
{id: 3, name: "lisa", items: "sales", code: "AU"},
{id: 4, name: "mano", items: "marketing", code: "IN"}
]
const map = {}
for (let item of list) {
if (map[item.items]) {
map[item.items].push(item);
} else {
map[item.items] = [item]
}
}
let maxLength = 0, result;
for (let key in map) {
if (map[key].length > maxLength) {
maxLength = map[key].length
result = map[key]
}
}
console.log(result)
这将 return
[
{ id: 1, name: 'dev', items: 'sales', code: 'IN' },
{ id: 3, name: 'lisa', items: 'sales', code: 'AU' }
]
如您所料。
我有一个对象数组,如果 属性 items
具有相同的值,return javascript
在下面的列表中,检索在
中具有相同值的对象数组items
属性 在 javascript
注意:也是动态对象数组,项目值可能会有所不同,所以不能直接用值过滤
var list =[
{id: 1, name: "dev", items: "sales", code: "IN"},
{id: 2, name: "lena", items: "finance", code: "SG"}
{id: 3, name: "lisa", items: "sales", code: "AU"},
{id: 4, name: "mano", items: "marketing", code: "IN"}
]
Expected Result
{id: 1, name: "dev", items: "sales", code: "IN"}
{id: 3, name: "lisa", items: "sales", code: "AU"},
var result= list.reduce((m, o) => {
const found= m.find(e => e.items === o.items);
if(found){
m.push(o);
return m
}
}, []);
使用 reduce 找出所有重复项会更麻烦。 你可以使用闭包。
const list = [
{ id: 1, name: "dev", items: "sales", code: "IN" },
{ id: 2, name: "lena", items: "finance", code: "SG" },
{ id: 3, name: "lisa", items: "sales", code: "AU" },
{ id: 4, name: "mano", items: "marketing", code: "IN" },
{ id: 5, name: "lisa", items: "gul gul", code: "AU" },
{ id: 6, name: "anthony", items: "some", code: "AU" },
{ id: 7, name: "mark", items: "gul gul", code: "AU" }
];
const findByItems = (eq) => (arr) => arr.filter(
(x, i) => arr.find((y, j) => i !== j && eq(x, y))
)
const duplicatedItems = findByItems((a, b) => a.items === b.items);
console.log(duplicatedItems(list))
我个人会根据 items 值对输入中的项目进行分组,然后提取最长的一组(在您的示例中,财务和营销将有一个项目只有销售会有两个):
var list = [
{id: 1, name: "dev", items: "sales", code: "IN"},
{id: 2, name: "lena", items: "finance", code: "SG"},
{id: 3, name: "lisa", items: "sales", code: "AU"},
{id: 4, name: "mano", items: "marketing", code: "IN"}
]
const map = {}
for (let item of list) {
if (map[item.items]) {
map[item.items].push(item);
} else {
map[item.items] = [item]
}
}
let maxLength = 0, result;
for (let key in map) {
if (map[key].length > maxLength) {
maxLength = map[key].length
result = map[key]
}
}
console.log(result)
这将 return
[
{ id: 1, name: 'dev', items: 'sales', code: 'IN' },
{ id: 3, name: 'lisa', items: 'sales', code: 'AU' }
]
如您所料。