最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove duplicate from array of objects based on value of properties in JavaScript - Stack Overflow

programmeradmin0浏览0评论

How can I remove duplicates from an array someArray like below based on the name property given the condition that if name is the same for two elements but for one them the type is new, the original one (without type new) will be retained?

someArray = [{id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 3, name:"apple", type: "new"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]

to

[{id: 1, name:"apple"}, {id: 2, name: "mango"}, {id: 4, name:"orange"}, {id: 6, name: "pineapple", type: "new"}]

How can I remove duplicates from an array someArray like below based on the name property given the condition that if name is the same for two elements but for one them the type is new, the original one (without type new) will be retained?

someArray = [{id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 3, name:"apple", type: "new"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]

to

[{id: 1, name:"apple"}, {id: 2, name: "mango"}, {id: 4, name:"orange"}, {id: 6, name: "pineapple", type: "new"}]

Share Improve this question asked May 16, 2022 at 1:44 SmarajitSmarajit 1731 gold badge4 silver badges14 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 6

You can use Map to club values by name and in case there are two values with same name just use the one without type = "new"

let someArray = [{id: 3, name:"apple", type: "new"}, {id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]

function getUnique(arr){
  let mapObj = new Map()
  
  arr.forEach(v => {
    let prevValue = mapObj.get(v.name)
    if(!prevValue || prevValue.type === "new"){
      mapObj.set(v.name, v)
    } 
  })
  return [...mapObj.values()]
}

console.log(getUnique(someArray))

You can use Array.prototype.reduce and filter out the items that satisfy the condition.

const 
  input = [
    { id: 1, name: "apple" },
    { id: 2, name: "mango" },
    { id: 3, name: "apple", type: "new" },
    { id: 4, name: "orange" },
    { id: 5, name: "orange", type: "new" },
    { id: 6, name: "pineapple", type: "new" },
  ],
  output = Object.values(
    input.reduce((r, o) => {
      if (!r[o.name] || (r[o.name].type === "new" && o.type !== "new")) {
        r[o.name] = o;
      }
      return r;
    }, {})
  );

console.log(output);

You can also do it using the Spread Syntax.

const 
  input = [
    { id: 1, name: "apple" },
    { id: 2, name: "mango" },
    { id: 3, name: "apple", type: "new" },
    { id: 4, name: "orange" },
    { id: 5, name: "orange", type: "new" },
    { id: 6, name: "pineapple", type: "new" },
  ],
  output = Object.values(
    input.reduce(
      (r, o) =>
        !r[o.name] || (r[o.name].type === "new" && o.type !== "new")
          ? { ...r, [o.name]: o }
          : r,
      {}
    )
  );

console.log(output);

To remove the duplicates from an array of objects:

  1. Create an empty array that will store the unique object IDs.
  2. Use the Array.filter() method to filter the array of objects.
  3. Only include objects with unique IDs in the new array.
// ✅ If you need to check for uniqueness based on a single property
const arr = [
  {id: 1, name: 'Tom'},
  {id: 1, name: 'Tom'},
  {id: 2, name: 'Nick'},
  {id: 2, name: 'Nick'},
];

const uniqueIds = [];

const unique = arr.filter(element => {
  const isDuplicate = uniqueIds.includes(element.id);

  if (!isDuplicate) {
    uniqueIds.push(element.id);

    return true;
  }

  return false;
});

// 
发布评论

评论列表(0)

  1. 暂无评论