Javascript-如何从数组中过滤自定义重复对象

时间:2020-11-07 18:18:17

标签: javascript arrays

基于这样的对象数组。我想过滤重复对象键,以便影响某些重复键,但不是每个人。

var arr = [
    {id: 1, value: 'John'},
    {id: 2, value: 'John'}, // Should be filtered
    {id: 3, value: 'John'}, // Should be filtered
    {id: 4, value: 'John'}, // Should be filtered
    {id: 5, value: 'Alex'},
    {id: 6, value: 'Louis'},
    {id: 7, value: 'David'},
    {id: 8, value: 'David'}, // Should not be filtered

]

结果:

arr = [
    {id: 1, value: 'John'},
    {id: 5, value: 'Alex'},
    {id: 6, value: 'Louis'},
    {id: 7, value: 'David'},
    {id: 8, value: 'David'},

]

此刻我尝试过:

arr = arr.reduce((a, b) => {
    if (!a.some(x => x.description === b.description)) a.push(b);
    return a;
}, []);

谢谢。

4 个答案:

答案 0 :(得分:2)

如果您想过滤重复项并保留第一个对象,可以对Set进行关闭以进行过滤。

const
    array = [{ id: 1, value: 'John' }, { id: 2, value: 'John' }, { id: 3, value: 'John' }, { id: 4, value: 'John' }, { id: 5, value: 'Alex' }, { id: 6, value: 'Louis' }, { id: 7, value: 'David' }, { id: 8, value: 'David' }],
    keep = ['David'],
    result = array.filter(
        (s => ({ value }) => keep.includes(value) || !s.has(value) && s.add(value))
        (new Set)
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

只需使用Set数据结构以及一些带有异常的数组或集合即可。

var arr = [
    {id: 1, value: 'John'},
    {id: 2, value: 'John'}, // Should be filtered
    {id: 3, value: 'John'}, // Should be filtered
    {id: 4, value: 'John'}, // Should be filtered
    {id: 5, value: 'Alex'},
    {id: 6, value: 'Louis'},
    {id: 7, value: 'David'},
    {id: 8, value: 'David'}, // Should not be filtered

]

const filterDuplicates = (arr, exceptions) => {
  const values = new Set();
  return arr.filter(item => {
    if(values.has(item.value) && !exceptions.includes(item.value)){
      return false;
    } else {
      values.add(item.value);
      return true;
    }
  })
}

console.log(filterDuplicates(arr, ["David"]));

答案 2 :(得分:0)

您可以将Array#filter与布尔值标志一起使用,以存储该值是否已经出现。

var arr = [
    {id: 1, value: 'John'},
    {id: 2, value: 'John'}, // Should be filtered
    {id: 3, value: 'John'}, // Should be filtered
    {id: 4, value: 'John'}, // Should be filtered
    {id: 5, value: 'Alex'},
    {id: 6, value: 'Louis'},
    {id: 7, value: 'David'},
    {id: 8, value: 'David'}, // Should not be filtered
];
const removeRepeats = (arr, val)=>{
  let found = false;
  return arr.filter(({value})=>value !== val || (!found && (found = true)));
};
console.log(removeRepeats(arr, 'John'));

答案 3 :(得分:0)

var arr = [
  { id: 1, value: "John" },
  { id: 2, value: "John" }, // Should be filtered
  { id: 3, value: "John" }, // Should be filtered
  { id: 4, value: "John" }, // Should be filtered
  { id: 5, value: "Alex" },
  { id: 6, value: "Louis" },
  { id: 7, value: "David" },
  { id: 8, value: "David" }, // Should not be filtered
];

const filtered = Array.from(
  arr
    .reduce((filtered, item) => {
      if (!filtered.has(item.value)) {
        filtered.set(item.value, item);
      }
      return filtered;
    }, new Map())
    .values()
);

console.log(filtered);