在对象数组中查找匹配属性(angular5)



我有一个包含对象的数组。我想知道对象的某些属性是否匹配,并将匹配项保存在新数组中。

示例:

[{id: 1, firstname: 'Bob', Lastname: 'Lupo'}, {id: 2, firstname: 'Tessa', Lastname: 'Moon'},
{id: 3, firstname: 'Erik' , Lastname: 'Deurne'}, {id: 4, firstname: 'Bob', Lastname: 'Lupo'}, {id: 5, firstname: 'Bob' , Lastname: 'Lupo'}]

我想知道对象firstname和lastname的2个属性是否与其他对象中的属性匹配,然后我想把匹配的id放在一个新数组中。

因此,在这种情况下,它将是[[1,4,5]]

有人知道怎么做吗?

Thx

试试这个代码

var array = [{id: 1, firstname: 'Bob', Lastname: 'Lupo'}, {id: 2, firstname: 'Tessa', Lastname: 'Moon'},
{id: 3, firstname: 'Erik' , Lastname: 'Deurne'}, {id: 4, firstname: 'Bob', Lastname: 'Lupo'}, {id: 5, firstname: 'Bob' , Lastname: 'Lupo'}]
let duplicates = {}
array.map(item => {
let key=item.firstname+'_'+item.Lastname
if( !duplicates[ key ]){
duplicates[ key ] = []
}
duplicates[ key ].push(item.id)
},{})
duplicateNamesIds = []
Object.values( duplicates ).forEach( value => {
if( value.length > 1 ){
duplicateNamesIds.push( value )
}
})
console.log(duplicateNamesIds)

var data = [{
id: 1,
firstname: 'Bob',
Lastname: 'Lupo'
}, {
id: 2,
firstname: 'Tessa',
Lastname: 'Moon'
},
{
id: 3,
firstname: 'Erik',
Lastname: 'Deurne'
}, {
id: 4,
firstname: 'Bob',
Lastname: 'Lupo'
}, {
id: 5,
firstname: 'Bob',
Lastname: 'Lupo'
}
];
var result = data.reduce((obj, val) => {
obj[val.firstname + "_" + val.Lastname] = obj[val.firstname + "_" + val.Lastname] || [];
obj[val.firstname + "_" + val.Lastname].push(val.id);
return obj;
}, {});
result = Object.values(result).filter(val=>val.length>1);
console.log(result);

步骤1是生成所有firstname-lastname组合及其id的树。我们可以在数据上的单个reduce中做到这一点。(注意:如果你能为你的对象想出一个安全使用的哈希函数,你不需要在这个树中有两个级别(

[ { id, firstname, lastname } ] -> { firstname: { lastname: [ id ] } }

console.log(
getData().reduce(
(tree, person) => {
if (!tree[person.firstname])
tree[person.firstname] = {};
if (!tree[person.firstname][person.Lastname]) 
tree[person.firstname][person.Lastname] = [];
tree[person.firstname][person.Lastname].push(person.id);
return tree;
}, {}
)
);
function getData() { return [{id: 1, firstname: 'Bob', Lastname: 'Lupo'}, {id: 2, firstname: 'Tessa', Lastname: 'Moon'},
{id: 3, firstname: 'Erik' , Lastname: 'Deurne'}, {id: 4, firstname: 'Bob', Lastname: 'Lupo'}, {id: 5, firstname: 'Bob' , Lastname: 'Lupo'}] };

一旦我们有了这棵树,我们就可以进入第二步。我们将树展平为id的集合。然后,我们使用filter:过滤掉所有只包含一个id的id集

{ firstname: { lastname: [ id ] } } -> [ [ id ] ]

const flattenTree = tree => Object.entries(tree)
.reduce(
(acc, [k, v]) => Object.entries(v).reduce(
(acc, [k, v]) => acc.concat([v]),
acc
),
[]
);

const duplicates = xxs => xxs.filter(xs => xs.length > 1);
console.log(
duplicates(
flattenTree(getTree())
)
);
function getTree() {
return getData().reduce(
(tree, person) => {
if (!tree[person.firstname])
tree[person.firstname] = {};
if (!tree[person.firstname][person.Lastname]) 
tree[person.firstname][person.Lastname] = [];
tree[person.firstname][person.Lastname].push(person.id);
return tree;
}, {}
)
};
function getData() { return [{id: 1, firstname: 'Bob', Lastname: 'Lupo'}, {id: 2, firstname: 'Tessa', Lastname: 'Moon'},
{id: 3, firstname: 'Erik' , Lastname: 'Deurne'}, {id: 4, firstname: 'Bob', Lastname: 'Lupo'}, {id: 5, firstname: 'Bob' , Lastname: 'Lupo'}] };

这个怎么样:

const data = [
{id: 1, firstname: 'Bob', Lastname: 'Lupo'}, 
{id: 2, firstname: 'Tessa', Lastname: 'Moon'},
{id: 3, firstname: 'Erik' , Lastname: 'Deurne'}, 
{id: 4, firstname: 'Bob', Lastname: 'Lupo'}, 
{id: 5, firstname: 'Bob' , Lastname: 'Lupo'}
]
// Definitely not the most beautiful code I've ever written
let result = Object.values(
data.reduce((a, c) => {
a[c.firstname] = a[c.firstname] || {};
a[c.firstname][c.Lastname] = [...(a[c.firstname][c.Lastname] || []), c.id];
return a;
}, []))
.map(o => [].concat(...Object.values(o)))
.filter(arr => arr.length > 1); 
console.log(result);

最新更新