为了账号安全,请及时绑定邮箱和手机立即绑定

如何计算数组中同名对象的数量?

如何计算数组中同名对象的数量?

达令说 2022-10-08 17:37:35
我有这个数组并试图获取具有相同name属性的对象的计数。我怎样才能做到这一点?也许以现代“es6”方式let cars = [    {id: 1, name: 'Mercedes', year: '2015'},    {id: 2, name: 'Mercedes', year: '2000'},    {id: 3, name: 'BMW', year: '2010'},    {id: 4, name: 'BMW', year: '2004'},    {id: 5, name: 'Volvo', year: '2012'},    {id: 6, name: 'Volvo', year: '2014'} ];
查看完整描述

5 回答

?
森栏

TA贡献1810条经验 获得超5个赞

这是适合您的解决方案:


let cars = [

  { id: 1, name: "Mercedes", year: "2015" },

  { id: 2, name: "Mercedes", year: "2000" },

  { id: 3, name: "BMW", year: "2010" },

  { id: 4, name: "BMW", year: "2004" },

  { id: 5, name: "Volvo", year: "2012" },

  { id: 6, name: "Volvo", year: "2014" },

];


let numbers = cars.reduce((acc, child) => {

  if (!acc[child.name]) {

    acc[child.name] = 0;

  }

  acc[child.name]++;

  return acc;

}, {});


console.log(numbers);


查看完整回答
反对 回复 2022-10-08
?
千巷猫影

TA贡献1829条经验 获得超7个赞

let cars = [

  { id: 1, name: "Mercedes", year: "2015" },

  { id: 2, name: "Mercedes", year: "2000" },

  { id: 3, name: "BMW", year: "2010" },

  { id: 4, name: "BMW", year: "2004" },

  { id: 5, name: "Volvo", year: "2012" },

  { id: 6, name: "Volvo", year: "2014" },

  { id: 7, name: "Volvo", year: "2012" },

  { id: 8, name: "Volvo", year: "2014" },

  { id: 9, name: "Volvo", year: "2012" }

];


let result = cars.reduce((acc, child, index) => {

    const indexFinded = acc.findIndex(el => el.name==child.name && el.year ==child.year);


    const result = {}


    if(indexFinded == -1) {

        result.name = child.name;

        result.year = child.year;

        result.qtd = 1;

        acc.push(result)

    } else {

        acc[indexFinded].qtd += 1

    }


    return acc;

}, []);


console.log(result);


查看完整回答
反对 回复 2022-10-08
?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

let cars = [

    {id: 1, name: 'Mercedes', year: '2015'},

    {id: 2, name: 'Mercedes', year: '2000'},

    {id: 3, name: 'BMW', year: '2010'},

    {id: 4, name: 'BMW', year: '2004'},

    {id: 5, name: 'Volvo', year: '2012'},

    {id: 6, name: 'Volvo', year: '2014'} 

];


let obj = {}


cars.forEach((item) => {

  //console.log(obj[item.name]) this return as undefined

  if (!obj[item.name]) {

    obj[item.name] = 1;

  } else {

    obj[item.name] += 1;

  }

})


console.log(obj)


查看完整回答
反对 回复 2022-10-08
?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

您可以使用一个简单的reduce函数,该函数将遍历每个对象并将计数加一


let cars = [

  { id: 1, name: "Mercedes", year: "2015" },

  { id: 2, name: "Mercedes", year: "2000" },

  { id: 3, name: "BMW", year: "2010" },

  { id: 4, name: "BMW", year: "2004" },

  { id: 5, name: "Volvo", year: "2012" },

  { id: 6, name: "Volvo", year: "2014" }

];


let count = cars.reduce((acc, child) => {

  acc[child.name] = (acc[child.name] || 0) + 1;

  return acc;

}, {});


console.log(count);


查看完整回答
反对 回复 2022-10-08
?
DIEA

TA贡献1820条经验 获得超2个赞

let counterObj = {}


let cars = [

    {id: 1, name: 'Mercedes', year: '2015'},

    {id: 2, name: 'Mercedes', year: '2000'},

    {id: 3, name: 'BMW', year: '2010'},

    {id: 4, name: 'BMW', year: '2004'},

    {id: 5, name: 'Volvo', year: '2012'},

    {id: 6, name: 'Volvo', year: '2014'} 

];



for (property of cars){

    counterObj[property.name] = 1 + (counterObj[property.name] || 0)

}


console.log(counterObj)


查看完整回答
反对 回复 2022-10-08
  • 5 回答
  • 0 关注
  • 165 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信