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

192
Visualizações
How to write customizer function for lodash _.mergeWith third argument in this situation?

I have two objects like that:

var obj3 = {
    customer_id: 1,
    products: { id: 1, name: "Car" },
  };

  var obj4 = {
    customer_id: 1,
    products: { id: 2, name: "Dress" },
  };

and the expected object is:

result = {
    customer_id: 1,
    products: [
      { id: 1, name: "Car" },
      { id: 2, name: "Dress" },
    ],
  };

How can i write function customizer(){} in this situation? Thanks so much for your help, and i'll be welcome for other solution with different way.

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

0

Merge the objects to a new object (the {}). Set the default values of the merged values to empty arrays. If the key is products concat them. If not return undefined, so the standard logic of _.merge() would be used:

const obj3 = { customer_id: 1, products: { id: 1, name: "Car" }, };
const obj4 = { customer_id: 1, products: { id: 2, name: "Dress" }, };

const result = _.mergeWith({}, obj3, obj4, (v1 = [], v2 = [], key) =>
  key === 'products' ? v1.concat(v2) : undefined
)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

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