Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
Remove duplicates from an array of objects and update key

I have an array of objects(shopping cart) and I want to remove duplicates that have same obj.id and obj.arr an empty array.After that to increase obj.quantity by one. Here is the original arr:

const cart = [
    {
        "id": 1,
        "name": "book",
        "arr": [],
        "quantity": 1
    },
    {
        "id": 2,
        "name": "pen",
        "arr": ["asa", "asd"],
        "quantity": 3
    },
    {
        "id": 1,
        "name": "book",
        "arr": [],
        "quantity": 1
    },
    {
        "id": 3,
        "name": "ball pen",
        "arr": ["azx", "dcv"],
        "quantity": 1
    }
]

Expected output should be like this:

const cart = [
    {
        "id": 1,
        "name": "book",
        "arr": [],
        "quantity": 2
    },
    {
        "id": 2,
        "name": "pen",
        "arr": ["asa", "asd"],
        "quantity": 3
    },
    {
        "id": 3,
        "name": "ball pen",
        "arr": ["azx", "dcv"],
        "quantity": 1
    }
]

I don't know how to update object.quantity after removing the duplicate. And if possible I want to keep the item with smallest index.
Could you please help me?
Thank you

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Modified code from How to remove all duplicates from an array of objects?. instead of filter I used reduce to then find and update the quantity of the duplicated item

const cart = [
    {
        "id": 1,
        "name": "book",
        "arr": [],
        "quantity": 1
    },
    {
        "id": 2,
        "name": "pen",
        "arr": ["asa", "asd"],
        "quantity": 3
    },
    {
        "id": 1,
        "name": "book",
        "arr": [],
        "quantity": 1
    },
    {
        "id": 3,
        "name": "ball pen",
        "arr": ["azx", "dcv"],
        "quantity": 1
    }
]

function findDupUpdateQty(arr){
return arr.reduce((acc, currentVal, index, self) => {
   if(index === self.findIndex((e)=> e.id === currentVal.id && e.arr.length === currentVal.arr.length)){
     acc.push(currentVal)
   } else {
   //if there's a duplicate then update quanitity
    acc[acc.findIndex((e)=> e.id === currentVal.id && e.arr.length === currentVal.arr.length)].quantity += currentVal.quantity
   }
   return acc
},[])
}

console.log(findDupUpdateQty(cart))

Edits with some of @Corrl suggestions:

const cart = [
    {
        "id": 1,
        "name": "book",
        "arr": [],
        "quantity": 1
    },
    {
        "id": 2,
        "name": "pen",
        "arr": ["asa", "asd"],
        "quantity": 3
    },
    {
        "id": 1,
        "name": "book",
        "arr": [],
        "quantity": 1
    },
    {
        "id": 3,
        "name": "ball pen",
        "arr": ["azx", "dcv"],
        "quantity": 1
    }
]

function findDupUpdateQty(arr){
return arr.reduce((acc, currentVal, index, self) => {                     //check if both arrays are the same
   let firstFoundIndex = self.findIndex((e)=> e.id === currentVal.id && JSON.stringify(e.arr) === JSON.stringify(currentVal.arr))
   if(index === firstFoundIndex){
     acc.push(currentVal)
   } else {
    self[firstFoundIndex].quantity += currentVal.quantity
   }
   return acc
},[])
}

console.log(findDupUpdateQty(cart))

about 4 years ago · Juan Pablo Isaza Relatório

0

To keep the order I would

  • build up a map with the id as key and an array with the objects as value
  • iterate this map and check for duplicates (= more than one object in array)
  • check for the empty array field on all duplicates
  • increase quantity on first occuring and overwrite existing entry (assuming all duplicates have the same quantity and it should only be increased by one, even if there are more than two duplicates)
  • rebuild cart

const cart = [{
  "id": 1,
  "name": "book",
  "arr": [],
  "quantity": 1
}, {
  "id": 2,
  "name": "pen",
  "arr": ["asa", "asd"],
  "quantity": 3
}, {
  "id": 1,
  "name": "book",
  "arr": [],
  "quantity": 1
}, {
  "id": 3,
  "name": "ball pen",
  "arr": ["azx", "dcv"],
  "quantity": 1
}]

function removeDuplicates(cart) {
  
  let map = new Map()

  // groupBy id
  cart.forEach(obj => {
    let entry = map.has(obj.id)
    if (entry) {
      let value = map.get(obj.id)
      map.set(obj.id, [...value, obj])
    } else {
      map.set(obj.id, [obj])
    }
  })


  map.forEach((value, key) => {

    // doubles?
    if (value.length > 1) {

      let allEmptyArrays = value.every(obj => obj.arr.length === 0)

      if (allEmptyArrays) {
        // keep only the first value
        let keepValue = value[0]
        // adjust quantity
        keepValue.quantity++
        // overwrite existing entry
        map.set(key, [keepValue])
      }
    }
  })

  // rebuild cart
  return Array.from(map).map(([_, value]) => value[0])
}

console.log(removeDuplicates(cart))

about 4 years ago · Juan Pablo Isaza Relatório

0

Thank you both!
I was planing to use one of your answers inside of selector(createSelector) from 'reselect' library. But looks like, it is incrementing obj.quantity, for all products, not only for those with obj.arr an empty array.
So I did more research, and I changed my addToCartReducer, based on this post. Now all the action is happening inside the reducer, not inside the selector. Every time I add to cart a product with obj.arr an empty array it increments its quantity by 1.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda