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

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

Javascript - 從對象數組中刪除重復的 ID

Javascript - 從對象數組中刪除重復的 ID

RISEBY 2021-11-12 17:45:22
取以下兩個數組:const array1 = [  {    props: {      type : 'text',      id : 'item1',      name : 'item1',      value : '@item1@',    },  },  {    props: {      type: 'hidden',      id: 'item2',      name: 'item2',      value: '@item2@',    },  }];const array2 = [  {    props: {      type: 'hidden',      id: 'item1',      name: 'item1',      value: '@item1@',    },  }];我想要做的是將它們連接成一個數組,并根據id屬性刪除任何重復項。不過這里需要說明的是,它的對象不具有type的hidden必須遵。所以我基本上應該留下 的內容array1,因為重復項 fromarray2的type值為hidden,如下所示:// Result[  {    props: {      type : 'text', // Note the type here is "text"      id : 'item1',      name : 'item1',      value : '@item1@',    },  },  {    props: {      type: 'hidden',      id: 'item2',      name: 'item2',      value: '@item2@',    },  }];我可以使用以下方法輕松連接它們:const array = array1.concat(array2);我的想法是然后使用過濾器,但我的大腦有點融化。這是我到目前為止的想法:const concat = (array1, array2) => {  const array = array1.concat(array2);  const ids = [];  // Create array of ID's  for (const i in array1) {    ids.push(array1[i].props.id);  }  return array.filter((obj) => {    if (obj.props.type !== 'hidden' && ids.includes(obj.props.id)) {      return true;    }    return false;  });};在這里使用Reduce會是更好的方法嗎?這是我到目前為止所擁有的 JSFiddle:https ://jsfiddle.net/64uprbhn/
查看完整描述

2 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

const array1 = [

  {

    props: {

      type : 'text',

      id : 'item1',

      name : 'item1',

      value : '@item1@',

    },

  },

  {

    props: {

      type: 'hidden',

      id: 'item2',

      name: 'item2',

      value: '@item2@',

    },

  }

];


const array2 = [

  {

    props: {

      type: 'hidden',

      id: 'item1',

      name: 'item1',

      value: '@item1@',

    },

  }

];


function getUnique(arr, comp) {


  const unique = arr

    .map(e => e[comp])


    // store the keys of the unique objects

    .map((e, i, final) => final.indexOf(e) === i && i)


    // eliminate the dead keys & store unique objects

    .filter(e => arr[e]).map(e => arr[e]);


  return unique;

}


const arr = array1.concat(array2);


console.log(getUnique(arr, 'id'));


查看完整回答
反對 回復 2021-11-12
?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

你可以

  1. id => object從第一個數組創建一個 Map

  2. 遍歷第二個數組,或者

    • 如果尚未在地圖中添加該項目

    • 檢查當前項目是否不是type: "hidden"并覆蓋另一個

    • 否則丟棄該項目

  3. 從 Map 創建一個新數組

const array1 = [

  {

    props: {

      type : 'text',

      id : 'item1',

      name : 'item1',

      value : '@item1@',

    },

  },

  {

    props: {

      type: 'hidden',

      id: 'item2',

      name: 'item2',

      value: '@item2@',

    },

  },

  {

    props: {

      type: 'hidden',

      id: 'item3',

      name: 'item3',

      value: '@item3@',

    },

  }

];


const array2 = [

  {

    props: {

      type: 'hidden',

      id: 'item1',

      name: 'item1',

      value: '@item1@',

    },

  },

  {

    props: {

      type: 'text',

      id: 'item3',

      name: 'item3',

      value: '@item3@',

    },

  }

];


//1. create a map from id => object pairs

const map = new Map(array1.map(obj => [obj.props.id, obj]));


//2. go over the second array

array2.forEach(obj => {

  if (!map.has(obj.props.id) //not a duplicate

    || obj.props.type !== "hidden") { //is not hidden

    //insert

    map.set(obj.props.id, obj);

  }

});


//3. convert back into array

const merged = [...map.values()];


console.log(merged);

為了記錄,您基本上可以使用相同(或非常相似)的東西,.filter但您必須O(n)對每個項目進行查找。Map 確保更快的查找。


查看完整回答
反對 回復 2021-11-12
  • 2 回答
  • 0 關注
  • 519 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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