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

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

合并分組的 2 個對象數(shù)組并在 JavaScript 上排除重復項

合并分組的 2 個對象數(shù)組并在 JavaScript 上排除重復項

慕沐林林 2023-11-02 20:10:00
我有 2 個具有父數(shù)組和子數(shù)組的分組數(shù)組,我想合并這兩個具有相同父數(shù)組的數(shù)組,但我僅復制具有唯一順序的子數(shù)組,我在互聯(lián)網(wǎng)上搜索了許多文章,但尚未找到解決方案。請幫我。這是我的代碼var groupOrder = [  {    group_time_str: "25 Apr 2019",    orders: [      {        vertical_id: "1",        vertical_category: "A"      },      {        vertical_id: "2",        vertical_category: "B"      }    ]  },  {    group_time_str: "26 Apr 2019",    orders: [      {        vertical_id: "1",        vertical_category: "A"      },      {        vertical_id: "2",        vertical_category: "B"      },      {        vertical_id: "3",        vertical_category: "C"      }    ]  }];var groupOrder2 = [  {    group_time_str: "26 Apr 2019",    orders: [      {        vertical_id: "3",        vertical_category: "C"      },      {        vertical_id: "4",        vertical_category: "D"      }    ]  },  {    group_time_str: "27 Apr 2019",    orders: [      {        vertical_id: "1",        vertical_category: "A"      },      {        vertical_id: "2",        vertical_category: "B"      }    ]  }];const combine = [...groupOrder, ...groupOrder2];const groupBy = combine.reduce((acc, cur) => {  acc[cur.group_time_str]    ? (acc[cur.group_time_str].orders = [        ...acc[cur.group_time_str].orders,        ...cur.orders      ])    : (acc[cur.group_time_str] = cur);  return acc;}, {});console.log(groupBy);body {  font-family: sans-serif;}<h1>Grouped Order</h1>您會看到2019 年 4 月 26 日組中有重復的{   vertical_id: "3",   vertical_category: "C"},我怎樣才能刪除重復的?請幫助我,謝謝
查看完整描述

2 回答

?
尚方寶劍之說

TA貢獻1788條經(jīng)驗 獲得超4個贊

嘗試減少第一個數(shù)組以僅獲取不匹配的項目,而不是在開頭合并數(shù)組:


const groupOrder = [{group_time_str: "25 Apr 2019",orders: [{vertical_id: "1",vertical_category: "A"},{vertical_id: "2",vertical_category: "B"}]},{group_time_str: "26 Apr 2019",orders: [{vertical_id: "1",vertical_category: "A"},{vertical_id: "2",vertical_category: "B"},{vertical_id: "3",vertical_category: "C"}]}];

const groupOrder2 = [{group_time_str: "26 Apr 2019",orders: [{vertical_id: "3",vertical_category: "C"},{vertical_id: "4",vertical_category: "D"}]},{group_time_str: "27 Apr 2019", orders: [{vertical_id: "1",vertical_category: "A"},{vertical_id: "2",vertical_category: "B"}]}];


const result = groupOrder.reduce((arr, g1) => {

  const g2match = groupOrder2.find(

    g2 => g2.group_time_str === g1.group_time_str

  );

  if (g2match) {

    const orders = g1.orders.filter(

      g1o =>

        !g2match.orders.find(

          m =>

            g1o.vertical_id === m.vertical_id &&

            g1o.vertical_category === m.vertical_category

        )

    );

    g2match.orders = [...g2match.orders, ...orders];

    return arr;

  }

  return [...arr, { ...g1 }];

}, []);


console.log([...result, ...groupOrder2]);


查看完整回答
反對 回復 2023-11-02
?
烙印99

TA貢獻1829條經(jīng)驗 獲得超13個贊

您可以執(zhí)行以下操作,過濾掉 cur.orders 以刪除重復項,


var groupOrder = [

  {

    group_time_str: "25 Apr 2019",

    orders: [

      {

        vertical_id: "1",

        vertical_category: "A"

      },

      {

        vertical_id: "2",

        vertical_category: "B"

      }

    ]

  },

  {

    group_time_str: "26 Apr 2019",

    orders: [

      {

        vertical_id: "1",

        vertical_category: "A"

      },

      {

        vertical_id: "2",

        vertical_category: "B"

      },

      {

        vertical_id: "3",

        vertical_category: "C"

      }

    ]

  }

];

var groupOrder2 = [

  {

    group_time_str: "26 Apr 2019",

    orders: [

      {

        vertical_id: "3",

        vertical_category: "C"

      },

      {

        vertical_id: "4",

        vertical_category: "D"

      }

    ]

  },

  {

    group_time_str: "27 Apr 2019",

    orders: [

      {

        vertical_id: "1",

        vertical_category: "A"

      },

      {

        vertical_id: "2",

        vertical_category: "B"

      }

    ]

  }

];

const combine = [...groupOrder, ...groupOrder2];

const groupBy = combine.reduce((acc, cur) => {

  acc[cur.group_time_str]

    ? (acc[cur.group_time_str].orders = [

        ...acc[cur.group_time_str].orders,

        ...cur.orders.filter(item => acc[cur.group_time_str].orders.findIndex(accOrder => item['vertical_id'] === accOrder['vertical_id'] && item['vertical_category'] === accOrder['vertical_category']) <= -1)

      ])

    : (acc[cur.group_time_str] = cur);

  return acc;

}, {});

console.log(groupBy);

body {

  font-family: sans-serif;

}

<h1>Grouped Order</h1>


查看完整回答
反對 回復 2023-11-02
  • 2 回答
  • 0 關(guān)注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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