第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

尚方寶劍之說 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"},];我想對兩個(gè)鍵(“from”和“to”)相同的所有唯一實(shí)例進(jìn)行排序和計(jì)數(shù)。我可以成功地計(jì)算每個(gè)案例,只比較一個(gè)鍵('from' 或 'to'),但我找不到關(guān)于如何比較兩者的解決方案。這是我的示例代碼: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);這是我現(xiàn)在得到的結(jié)果(因?yàn)榇a只比較“來自”鍵):console.log(arr2);// Array(2)// 0: {from: "Monica", to: "Rachel", count: 4}// 1: {from: "Ross", to: "Chandler", count: 2}// length: 2這是我想要達(dá)到的結(jié)果: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貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊

  • 使用Array.reduce,您可以通過fromto變量對鍵對輸入數(shù)組進(jìn)行分組。

  • 對于重復(fù),計(jì)數(shù)將增加,最后,結(jié)果將存儲在groupedBy對象的每個(gè)鍵的值上。

  • 您只能使用 提取值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);


查看完整回答
反對 回復(fù) 2023-05-19
?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以使用帶有分隔符的組合鍵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; }


查看完整回答
反對 回復(fù) 2023-05-19
?
繁星點(diǎn)點(diǎn)滴滴

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊

替代:

  1. from通過and組合對數(shù)組進(jìn)行排序to,在此之后,重復(fù)項(xiàng)將在排序后的數(shù)組中相鄰。

  2. 遍歷排序的數(shù)組以刪除重復(fù)項(xiàng)和計(jì)數(shù)。

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


查看完整回答
反對 回復(fù) 2023-05-19
?
侃侃無極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

循環(huán)每個(gè)對象,獲取它的值from并to進(jìn)行比較。


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

  }

}


查看完整回答
反對 回復(fù) 2023-05-19
  • 4 回答
  • 0 關(guān)注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號