Home > Enterprise >  Remove duplicate from array of objects based on value of properties in JavaScript
Remove duplicate from array of objects based on value of properties in JavaScript

Time:05-16

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"}]

CodePudding user response:

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);

CodePudding user response:

You can iterate your array and evaluate if is 'type: new' and is not already existing, then pop out the item.

let 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"
}];
console.info('Initial value:'   JSON.stringify(someArray));
someArray.forEach(function(item, index, object) {
    if (item.type === 'new' && someArray.filter(val => val.name === item.name && !val.type)) {
        object.splice(index, 1);
    }
});

console.info('Desired Result:'   JSON.stringify(someArray));

CodePudding user response:

let 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"}]

const result = someArray.reduce((resArr, currentArr) => {
  let other = resArr.some((ele) => currentArr.name === ele.name)
  if (!other) resArr.push(currentArr)
  return resArr
}, [])
console.log(result)

CodePudding user response:

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))

  • Related