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

时间:2018-07-20 12:13:14

标签: javascript arrays angular object match

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

示例:

[{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'}]

我想知道对象名字和姓氏的2个属性是否与另一个对象中的属性匹配,然后我想将匹配的id放入新数组中。

所以在这种情况下,它应该是[[1,4,5]]

有人知道这样做的方法吗?

Thx

4 个答案:

答案 0 :(得分:1)

尝试此代码

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)

答案 1 :(得分:0)

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);

答案 2 :(得分:0)

第1步是创建所有firstname-lastname组合及其ID的树。我们可以在单个reduce数据上执行此操作。 (注意:如果您可以为您的对象提供一个安全使用的哈希函数,则在此树中不需要2个级别)

[ { 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'}] };

有了这棵树后,我们可以转到步骤2。我们将树展平为仅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'}] };

答案 3 :(得分:0)

如何?

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);