【问题标题】:How to remove the repeating `place` name which contains the same `name`?如何删除包含相同“名称”的重复“地点”名称?
【发布时间】:2023-03-09 07:50:01
【问题描述】:

我发现以下answer 对删除包含重复项的重复对象数组有很大帮助。

我已经为我修改的示例制作了fork

相关功能:

const uniqueArray = things.thing.filter((thing,index) => {
  return index === things.thing.findIndex(obj => {
    return JSON.stringify(obj) === JSON.stringify(thing);
  });
});

例如我有:

[
  {"place":"here","name":"stuff"},
  {"place":"there","name":"morestuff"},
  {"place":"there","name":"morestuff"},
  {"place":"herehere","name":"stuff"}
]

它会返回:

[
  {"place":"here","name":"stuff"},
  {"place":"there","name":"morestuff"},
  {"place":"herehere","name":"stuff"}
]

如何删除包含相同name 的重复place 名称?

预期输出:

[
  {"place":"here","name":"stuff"},
  {"place":"there","name":"morestuff"}
]

【问题讨论】:

  • 您可能希望使用 Array.reduce 代替,其中累加器是“过滤”对象,只有当累加器不包含所需项目时才会推送下一个项目。 const uniqueArray = things.thing.reduce((acc, next) => { if (acc.find(i => i.place === next.place)) return acc; else return (acc.push(next), acc); }, []);
  • 对不起,我忘了放预期的输出。

标签: javascript arrays object duplicates


【解决方案1】:

您可以reduce 覆盖对象数组。简单来说,如果累加器中已经存在键值与当前对象相同的对象,就不要再添加了。

这里有一个函数可以让您指定要删除的密钥:

const arr = [
  {"place":"here","name":"stuff"},
  {"place":"there","name":"morestuff"},
  {"place":"there","name":"morestuff"},
  {"place":"herehere","name":"stuff"}
];

// Accepts an array and a key that should have the
// duplicates removed
function remove(arr, key) {

  // Iterate over the array passing in the accumulator
  // and the current element
  return arr.reduce((acc, c) => {

    // If there is an object in the accumulator with the
    // same key value as the current element simply return the
    // accumulator
    if (acc.find(obj => obj[key] === c[key])) return acc;

    // Otherwise add the current element to the accumulator
    // and return it
    return acc.concat(c);
  }, []);
}

function showJSON(arr, id) {
  const json = JSON.stringify(arr, null, 2);
  document.querySelector(`#${id} code`).textContent = json;
}

// remove duplicate places
showJSON(remove(arr, 'place'), 'places');

// remove duplicate names
showJSON(remove(arr, 'name'), 'names');
<div id="places">
Removed duplicate places
<pre><code></code></pre>
</div>

<div id="names">
Removed duplicate names
<pre><code></code></pre>
</div>

【讨论】:

    【解决方案2】:

    检查一下

      const things = [
            {"place":"here","name":"stuff"},
            {"place":"there","name":"morestuff"},
            {"place":"there","name":"morestuff"},
            {"place":"herehere","name":"stuff"}
        ]
    
        const uniqueArray = things.reduce((accumulator, currentValue) => {
            if (accumulator.find(a => a.name === currentValue.name))
                return accumulator;
            else
                return (accumulator.push(currentValue), accumulator);
        }, []);
    

    输出

        [ { place: 'here', name: 'stuff' },
          { place: 'there', name: 'morestuff' } ]
    

    【讨论】:

    【解决方案3】:

    您可以将数组归约与过滤器一起使用

    let data=[
      {"place":"here","name":"stuff"},
      {"place":"there","name":"morestuff"},
      {"place":"there","name":"morestuff"},
      {"place":"herehere","name":"stuff"}
    ]
    
    // Using reduce() to separate the contents we want
    let result=data.reduce((acc,value)=>{
      if(acc.filter(val=>val.name==value.name).length==0) // checking the accumulator if it already containsa the value
      {
        acc.push(value); // if the array returned is of length==0 we can push in it
      }
    return acc;
    
    },[])
    console.log(result);

    Array FilterArray.prototype.Reduce

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 2020-06-18
      • 2021-11-26
      • 2015-08-04
      • 2017-02-18
      • 2020-03-30
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多