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

87
Visualizações
How can I compare two objects and add the missed values?

I would like to compare two objects by the object value "Datum". In my case I would like to check if the "Datum" value in "mengeGes" is also in the "v" object. If not, then add the values of the "mengeGes" object to the "v" object.

In my example, the object with the "Datum" 2022-02-22 should add from the mengeGes to v:

const mengeGes = [{
    "Datum": "2022-02-20",
    "Datum_str": "20.02.2022"
  },
  {
    "Datum": "2022-02-21",
    "Datum_str": "21.02.2022"
  },
  {
    "Datum": "2022-02-22",
    "Datum_str": "22.02.2022"
  }
]


var v = [{
    "Datum": "2022-02-20",
    "Datum_str": "20.02.2022",
    "xx": 4
  },
  {
    "Datum": "2022-02-21",
    "Datum_str": "21.02.2022",
    "xx": 4
  }
]

// The expected result:
v = [{
    "Datum": "2022-02-20",
    "Datum_str": "20.02.2022",
    "xx": 4
  },
  {
    "Datum": "2022-02-21",
    "Datum_str": "21.02.2022",
    "xx": 4
  },
  {
    "Datum": "2022-02-22",
    "Datum_str": "22.02.2022"
  }
]

console.log(v)

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

0

We can create an array containing the 'missing' objects like so:

var missing = mengeGes.filter(menge => v.filter(vv => vv.Datum === menge.Datum).length === 0);

Here we loop over mengeGes and filter it by checking if there is an object in v where Datum maches


Then, we can combine that array with the existing v:

var combine = [ ...v, ...missing ];

const mengeGes = [{
    "Datum": "2022-02-20",
    "Datum_str": "20.02.2022"
  },
  {
    "Datum": "2022-02-21",
    "Datum_str": "21.02.2022"
  },
  {
    "Datum": "2022-02-22",
    "Datum_str": "22.02.2022"
  }
]

var v = [{
    "Datum": "2022-02-20",
    "Datum_str": "20.02.2022",
    "xx": 4
  },
  {
    "Datum": "2022-02-21",
    "Datum_str": "21.02.2022",
    "xx": 4
  }
];

var missing = mengeGes.filter(menge => v.filter(vv => vv.Datum === menge.Datum).length === 0);
var combine = [ ...v, ...missing ];
console.log(combine);


Result:

[
  {
    "Datum": "2022-02-20",
    "Datum_str": "20.02.2022",
    "xx": 4
  },
  {
    "Datum": "2022-02-21",
    "Datum_str": "21.02.2022",
    "xx": 4
  },
  {
    "Datum": "2022-02-22",
    "Datum_str": "22.02.2022"
  }
]
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