Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

82
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!