将计数值添加到具有相同键值的对象(对象数组)

时间:2019-04-24 11:10:32

标签: javascript arrays loops

你好,我要对一系列对象进行复杂的迭代。我有这样的数组:

[ 
  { name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
  { name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },
  { name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },
  { name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },
  { name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },
  { name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
  { name: 'Ann', lastName: 'Smith', dob: '2010-11-29' },
]

我想将count属性添加到对具有相同名称和姓氏的对象进行计数的每个对象中...因此现在应该是:

[ 
  { name: 'Jacob', lastName: 'Smith', count: 4 },
  { name: 'Ann', lastName: 'Smith', count: 2 },
  { name: 'Ann', lastName: 'Nansen', count: 1' },
]

5 个答案:

答案 0 :(得分:2)

您可以使用Array.reduceObject.values

  • 使用键作为名称和姓氏组合转换对象中的数组,并将值作为结果对象。
  • 从对象中获取所有值作为最终结果

let arr = [{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },{ name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },{ name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Ann', lastName: 'Smith', dob: '2010-11-29' }];

let result = Object.values(arr.reduce((a,{name, lastName}) => {
  let key = `${name}_${lastName}`;
  a[key] = a[key] || {name, lastName, count : 0};
  a[key].count++;
  return a;
}, {}));

console.log(result);

答案 1 :(得分:0)

  const hash = [];

  for(const { name, lastName } of persons) {
    const key = name + "/" + lastName;
    if(!hash[key]) hash[key] = {
      name,
      lastName,
      count: 0,
    };

    hash[key].count++;
 }

  const result = Object.values(hash);

答案 2 :(得分:0)

您可以使用JSON.stringify以安全的方式组合姓名和姓氏。我喜欢使用Map将具有相同键的记录分组在一起:

const data = [{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },{ name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },{ name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Ann', lastName: 'Smith', dob: '2010-11-29' }];
const keyed = data.map(o => [JSON.stringify([o.name, o.lastName]), o]);
const map = new Map(keyed.map(([key, {name, lastName}]) => 
                              [key, {name, lastName, count: 0}]));
keyed.forEach(([key, o]) => map.get(key).count++);
const result = Array.from(map.values());

console.log(result);

答案 3 :(得分:0)

let arr=[ 
  { name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
  { name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },
  { name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },
  { name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },
  { name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },
  { name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
  { name: 'Ann', lastName: 'Smith', dob: '2010-11-29' },
];



let outerArr=[];

for(arrValue of arr)
{
  delete arrValue.dob

  let index=outerArr.findIndex(item=> item.name==arrValue.name && 
  item.lastName==arrValue.lastName);

  if(index==-1)
  {
	let arrFind=arr.filter(item=> item.name==arrValue.name && 
    item.lastName==arrValue.lastName)
     arrValue.count=arrFind.length
     outerArr.push(arrValue)
  }

}
	console.log('result',outerArr)

答案 4 :(得分:0)

您可以通过减少原始Array来实现。 当您遍历people时,可以检查是否已经使用Array.some对其进行了“分组”-如果尚未将其构建的对象推入先前返回的数组中。

const getInstances = ({ name, lastName }, data) => data.filter(d => d.name === name && d.lastName === lastName).length

const people = [ 
  { name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
  { name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },
  { name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },
  { name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },
  { name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },
  { name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
  { name: 'Ann', lastName: 'Smith', dob: '2010-11-29' },
]
  
const groupedPeople = people.reduce((group, person, i, people) => {
  const alreadyBeenGrouped = group.some(({ name, lastName }) => name === person.name && lastName === person.lastName)

  if (!alreadyBeenGrouped) {
    group.push({
      name: person.name,
      lastName: person.lastName,
      count: getInstances(person, people)
    })
  }

  return group
}, [])

console.log(groupedPeople)