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

时间:2019-04-09 10:39:51

标签: javascript arrays object duplicates

我发现以下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"}
]

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

预期输出:

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

3 个答案:

答案 0 :(得分: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' } ]

答案 1 :(得分:2)

您可以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 :(得分:0)

您可以将array reduce与filter一起使用

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