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

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

在 JavaScript/Node.js 中列出對(duì)象數(shù)組中的重復(fù)條目

在 JavaScript/Node.js 中列出對(duì)象數(shù)組中的重復(fù)條目

慕蓋茨4494581 2022-12-22 15:15:17
offers我有一個(gè)包含如下對(duì)象的數(shù)組:{     sellItem: {        id: _,         quantity: _    },     buyItem: {        id: _,         quantity: _    },     volume: _}我想找到重復(fù)項(xiàng) - 意思是具有相同“銷(xiāo)售 ID”和“購(gòu)買(mǎi) ID”的報(bào)價(jià)。我還想在原始“優(yōu)惠”數(shù)組中記錄這些項(xiàng)目的索引。我嘗試這樣做了 2 天,但沒(méi)有成功,因?yàn)槲野l(fā)現(xiàn)很難用可管理的行數(shù)來(lái)寫(xiě)我想做的事情。StackOverflow 上的其他問(wèn)題只涉及一個(gè)對(duì)象,而不是嵌套對(duì)象。我的offers數(shù)組的示例:{sellItem: {id: Pizza, quantity: 2}, buyItem: {id: Dollar, quantity: 1}, volume: 1}{sellItem: {id: Pizza, quantity: 3}, buyItem: {id: Dollar, quantity: 2}, volume: 1}{sellItem: {id: Banana, quantity: 2}, buyItem: {id: Pound, quantity: 1}, volume: 1}{sellItem: {id: Apple, quantity: 2}, buyItem: {id: Euro, quantity: 1}, volume: 1}{sellItem: {id: Pizza, quantity: 5}, buyItem: {id: Dollar, quantity: 3}, volume: 1}這里的預(yù)期結(jié)果是:0: Selling 2x Pizza for 1x Dollar1: Selling 3x Pizza for 2x Dollar4: Selling 5x Pizza for 3x Dollar“優(yōu)惠”數(shù)組中的所有其他條目都應(yīng)忽略,因?yàn)樗鼈儾皇侵貜?fù)的優(yōu)惠。
查看完整描述

1 回答

?
四季花海

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

您可以使用filteralong withfind來(lái)搜索具有相同id.


const arr = [{sellItem: {id: 'Pizza', quantity: 2}, buyItem: {id: 'Dollar', quantity: 1}, volume: 1},

{sellItem: {id: 'Pizza', quantity: 3}, buyItem: {id: 'Dollar', quantity: 2}, volume: 1},

{sellItem: {id: 'Banana', quantity: 2}, buyItem: {id: 'Pound', quantity: 1}, volume: 1},

{sellItem: {id: 'Apple', quantity: 2}, buyItem: {id: 'Euro', quantity: 1}, volume: 1},

{sellItem: {id: 'Pizza', quantity: 5}, buyItem: {id: 'Dollar', quantity: 3}, volume: 1}];

const res = arr.filter(({sellItem: {id}},idx)=>

   arr.find(({sellItem:{id:id2}},idx2) => idx !== idx2 && id === id2));

console.log(res);


查看完整回答
反對(duì) 回復(fù) 2022-12-22
  • 1 回答
  • 0 關(guān)注
  • 137 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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