javascript - 使用相同键值( 对象数组) 将javascript添加到对象

您好,我有一些复杂的迭代要在一个对象数组上完成。 我有这样的数组:


[ 


 { 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' },


]



时间:

你可以使用 Array.reduceObject.values

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

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

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



你可以使用 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);

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)

你可以通过减少原始数组来实现这一点。 当你遍历 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)
...