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

116
Views
Merge objects from two arrays of object based in the index

i searched a lot about this but i do not find anything that can enlight me about my issue:

I have this code:

let array1 = ["a", "b", 3, {
    p1: 'hola'
  }, "c", "d"],
  array2 = [1, 2, {
    p1: 'adios'
  }],
  result = [],
  i, l = Math.min(array1.length, array2.length);


for (i = 0; i < l; i++) {
  if (typeof array1[i] === 'object' && typeof array2[i] === 'object') {
    result.push(array2[i], ...(JSON.stringify() === JSON.stringify() ?
      [] :
      [array1[i]]
    ));
  } else {
    result.push(array2[i], array1[i]);
  }

}
result.push(...array1.slice(l), ...array2.slice(l));

console.log(result);

i have modified the code with the suggestions, right now the code does this:

we have two array;

array1 = ["a", "b", 3, {p1: 'hello'},"c", "d"] array2 = [1, 2, {p1: 'hello'}]

the result right now is this based in the code:

result: [1, 'a', 2, 'b', {p1: 'hello'}, 3, {p1: 'hello'}, 'c', 'd']

this work fine because i dont want to omit objects that are in different index between the two array, the problem right now is that when the objects in the two array are in the same index this code;

array1 = ["a", "b", {p2: 'goodbye'},"c", "d"] array2 = [1, 2, {p1: 'hello'}]

result : [1, 'a', 2, 'b', {p1: 'hello'}, 'c', 'd']

This is my issue right now, what i want is that when there are object in the same index on two array compare the properties of the objects and is the same skip the first array object and pass the second to the final array, but if the properties are not the same, combine the properties of the object in one, this is the ideal result that i want:

array1 = ["a", "b", {p2: 'goodbye'},"c", "d"] array2 = [1, 2, {p1: 'hello'}]

result : [1, 'a', 2, 'b', {p1: 'hello', p2: 'goodbye'}, 'c', 'd']

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could compare the items and if same omit the second item.

let array1 = ["a", "b", {p1: 'hello world'},"c", "d"],
    array2 = [1, 2,  {p1: 'hello world'}],
    result = [],
    i, l = Math.min(array1.length, array2.length);
    
for (i = 0; i < l; i++) {
    result.push(array2[i], ...(JSON.stringify() === JSON.stringify()
        ? []
        : [array1[i]]
    ));
}
result.push(...array1.slice(l), ...array2.slice(l));

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

I think this is what you're after.

let array1 = ['a', 'b', { p2: 'goodbye' }, 'c', 'd'];
let array2 = [1, 2, { p1: 'hello' }];

let result = [];
for (let i = 0; i < Math.max(array1.length, array2.length); i++) {
  if (typeof array1[i] == 'object' && typeof array2[i] == 'object') {
    result.push({ ...array2[i], ...array1[i] });
  } else {
    array2[i] && result.push(array2[i]);
    array1[i] && result.push(array1[i]);
  }
}

console.log(result);

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!