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

寻找一种方法来比较和计算数组中所有嵌套对象的键/值对

寻找一种方法来比较和计算数组中所有嵌套对象的键/值对

尚方宝剑之说 2023-05-19 15:06:56
我有一系列对象,例如:const arr1 = [ {"from":"Monica","to":"Rachel"}, {"from":"Monica","to":"Rachel"}, {"from":"Monica","to":"Chandler"}, {"from":"Monica","to":"Chandler"}, {"from":"Ross","to":"Chandler"}, {"from":"Ross","to":"Monica"},];我想对两个键(“from”和“to”)相同的所有唯一实例进行排序和计数。我可以成功地计算每个案例,只比较一个键('from' 或 'to'),但我找不到关于如何比较两者的解决方案。这是我的示例代码:let arr2 = Object.values(arr1.reduce((c, { from, to }) => { c[from] = c[from] || { from, to, count: 0 }; c[from].count++; return c;}, {}));console.log(arr2);这是我现在得到的结果(因为代码只比较“来自”键):console.log(arr2);// Array(2)// 0: {from: "Monica", to: "Rachel", count: 4}// 1: {from: "Ross", to: "Chandler", count: 2}// length: 2这是我想要达到的结果:console.log(arr2);// Array(2)// 0: {from: "Monica", to: "Rachel", count: 2}// 1: {from: "Monica", to: "Chandler", count: 2}// 2: {from: "Ross", to: "Chandler", count: 1}// 3: {from: "Ross", to: "Monica", count: 1}// length: 4
查看完整描述

4 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

  • 使用Array.reduce,您可以通过fromto变量对键对输入数组进行分组。

  • 对于重复,计数将增加,最后,结果将存储在groupedBy对象的每个键的值上。

  • 您只能使用 提取值Object.values

const arr1 = [

 {"from":"Monica","to":"Rachel"},

 {"from":"Monica","to":"Rachel"},

 {"from":"Monica","to":"Chandler"},

 {"from":"Monica","to":"Chandler"},

 {"from":"Ross","to":"Chandler"},

 {"from":"Ross","to":"Monica"},

];


const groupedBy = arr1.reduce((acc, cur) => {

  const key = `${cur.from}_${cur.to}`;

  acc[key] ? acc[key].count ++ : acc[key] = {

    ...cur,

    count: 1

  };

  return acc;

}, {});

const result = Object.values(groupedBy);

console.log(result);


查看完整回答
反对 回复 2023-05-19
?
慕容3067478

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

您可以使用带有分隔符的组合键from和值。to


const

    array = [{ from: "Monica", to: "Rachel" }, { from: "Monica", to: "Rachel" }, { from: "Monica", to: "Chandler" }, { from: "Monica", to: "Chandler" }, { from: "Ross", to: "Chandler" }, { from: "Ross", to: "Monica" }],

    result = Object.values(array.reduce((r, { from, to }) => {

        const key = [from, to].join('|');

        r[key] ??= { from, to, count: 0 };

        r[key].count++;

        return r;

    }, {}));


console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反对 回复 2023-05-19
?
繁星点点滴滴

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

替代:

  1. from通过and组合对数组进行排序to,在此之后,重复项将在排序后的数组中相邻。

  2. 遍历排序的数组以删除重复项和计数。

const arr1 = [

 {"from":"Monica","to":"Rachel"},

 {"from":"Monica","to":"Rachel"},

 {"from":"Monica","to":"Chandler"},

 {"from":"Monica","to":"Chandler"},

 {"from":"Ross","to":"Chandler"},

 {"from":"Ross","to":"Monica"},

];

 

var arr2 = arr1.sort((obj1, obj2) => (obj1.from + obj1.to).localeCompare(obj2.from + obj2.to));


for(var i = arr2.length - 1; i >= 0; i--){

  if(i > 0 && arr2[i].from + arr2[i].to == arr2[i-1].from + arr2[i-1].to){

     arr2[i-1].count = arr2[i].count ? arr2[i].count + 1 : 2;

     arr2.splice(i, 1);

  }else if(!arr2[i].count){

    arr2[i].count = 1;

  }

}

console.log(arr2);


查看完整回答
反对 回复 2023-05-19
?
侃侃无极

TA贡献2051条经验 获得超10个赞

循环每个对象,获取它的值from并to进行比较。


const arr = [

 {"from": "A", "to": "B"},

 {"from": "A", "to": "C"},

 {"from": "A", "to": "A"},

];


for (var i = 0; i < arr.length; i++) {

  if (arr[i].from == arr[i].to) {

    console.log('Matches!');

  }

  else {

    console.log('No match');

  }

}


查看完整回答
反对 回复 2023-05-19
  • 4 回答
  • 0 关注
  • 93 浏览
慕课专栏
更多

添加回答

举报

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