Home > Software design >  Merge objects from two arrays of object based in the index
Merge objects from two arrays of object based in the index

Time:11-07

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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']

CodePudding user response:

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related