如果存在于另一个对象数组中,则从对象数组内部的数组中删除元素

时间:2018-08-23 01:52:52

标签: javascript arrays algorithm object

我正在处理大量对象。我将数据结构简化为以下内容。每个对象都有一个id,每个id都有两个与其关联的数组type1type2

const arr = [{id: "12345", type1: ["Hat 1", "Hat 3"], type2: ["Hat 2", "Glove 4"]}, 
             {id: "12345", type1: ["Glove 1", "Hat 1"], type2: ["Glove 3", "Hat 2"]},           
             {id: "54321", type1: ["Jacket 1", "Hat 4"], type2: ["Hat 3", "Hat 4"]},
             {id: "54321", type1: ["Glove 2", "Hat 2"], type2: ["Glove 3", "Jacket 4"]},
             {id: "13579", type1: ["Hat 1", "Hat 2"], type2: ["Hat 3", "Hat 4"]},
             {id: "13579", type1: ["Glove 1", "Glove 2"], type2: ["Glove 3", "Glove 4"]}]

我有一个“查找”对象数组。每个对象都有一个id和一个title

const lookup = [{id: "12345", title: "Hat 1"},
                {id: "12345", title: "Hat 2"},
                {id: "12345", title: "Glove 3"},
                {id: "54321", title: "Hat 3"} 
                {id: "54321", title: "Jacket 4"},
                {id: "54321", title: "Glove 5"},
                {id: "13579", title: "Hat 2"},
                {id: "13579", title: "Jacket 3"}]

我需要对所有具有title的匹配ID使用“ lookup”对象,我需要将其从type1或type2或两者中删除。所以我得到的对象数组看起来像这样

const result = [{id: "12345", type1: ["Hat 3"], type2: ["Glove 4"]}, 
                {id: "12345", type1: ["Glove 1"], type2: []},           
                {id: "54321", type1: ["Jacket 1", "Hat 4"], type2: ["Hat 4"]},
                {id: "54321", type1: ["Glove 2", "Hat 2"], type2: ["Glove 3"]},
                {id: "13579", type1: ["Hat 1"], type2: ["Hat 3", "Hat 4"]},
                {id: "13579", type1: ["Glove 1", "Glove 2"], type2: ["Glove 3", "Glove 4"]}]

重复项并且必须在两个数组中搜索任何匹配的ID,这使我感到困惑。有没有简单的方法可以做到这一点,或者有更好的方法来结构化数据,使数据不会那么复杂?

2 个答案:

答案 0 :(得分:1)

遍历arr,并为arr的每个条目遍历lookup以比较和修改arr

  for(let arrEntry of arr) {
    let id = arrEntry.id;
    let type1 = arrEntry.type1;
    let type2 = arrEntry.type2;

    for(let lookupEntry of lookup) {
      let title = lookupEntry.title;
      if(lookupEntry.id === id && type1.includes(title)) {
        type1.splice(type1.indexOf(title), 1);
      }
      if(lookupEntry.id === id && type2.includes(title)) {
        type2.splice(type2.indexOf(title), 1);
      }
    }
  }

  console.log(arr)

答案 1 :(得分:1)

我们不要突变原始数据,而是创建一个新的结果数组。您可以使用map

中的filtersomeArray
arr.map(({id, type1, type2}) => ({
    id,
    type1: type1.filter(t => !lookup.some(l => id===l.id && l.title === t)),
    type2: type2.filter(t => !lookup.some(l => id===l.id && l.title === t))
}));

这是一个有效的示例:

const arr =[
     {id: "12345", type1: ["Hat 1", "Hat 3"], type2: ["Hat 2", "Glove 4"]}, 
     {id: "12345", type1: ["Glove 1", "Hat 1"], type2: ["Glove 3", "Hat 2"]},           
     {id: "54321", type1: ["Jacket 1", "Hat 4"], type2: ["Hat 3", "Hat 4"]},
     {id: "54321", type1: ["Glove 2", "Hat 2"], type2: ["Glove 3", "Jacket 4"]},
     {id: "13579", type1: ["Hat 1", "Hat 2"], type2: ["Hat 3", "Hat 4"]},
     {id: "13579", type1: ["Glove 1", "Glove 2"], type2: ["Glove 3", "Glove 4"]}
  ],
  lookup = [
     {id: "12345", title: "Hat 1"},
     {id: "12345", title: "Hat 2"},
     {id: "12345", title: "Glove 3"},
     {id: "54321", title: "Hat 3"}, 
     {id: "54321", title: "Jacket 4"},
     {id: "54321", title: "Glove 5"},
     {id: "13579", title: "Hat 2"},
     {id: "13579", title: "Jacket 3"}
  ],
  res = arr.map(({id, type1, type2}) => ({
	  id,
	  type1: type1.filter(t => !lookup.some(l => id===l.id && l.title === t)),
	  type2: type2.filter(t => !lookup.some(l => id===l.id && l.title === t))
  }));
  
  console.log(res);