亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

尋找一種方法來比較和計算數組中所有嵌套對象的鍵/值對

尋找一種方法來比較和計算數組中所有嵌套對象的鍵/值對

尚方寶劍之說 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 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號