-1

I have two multidimensional arrays:

const parent = [['ff@gmail.com', 'a@a.com', 'f@f.com'], ['ff@gmail.com']];
const customers = [['hh@gmail.com'], ['gg@gmail.com']];

I would like to compare the parent[0] with customers[0] and parent[1] to customers[1] and if the customers is not contain the parent element push it to customer.

what i try:

for(let x= 0; x < parent.length; x++){
 const innerArray = parent[x].length
  for(let y = 0; y < innerArray; y++){
        for (const customer of customers){
        if (!customer.includes(parent[x][y])){
        customer.push(parent[x][y])
      }
    }
  }
}

console.log(customers)

the above code output is:

[["hh@gmail.com", "ff@gmail.com", "a@a.com", "f@f.com"], ["gg@gmail.com", "ff@gmail.com", "a@a.com", "f@f.com"]]

what i would like to output is :

[["hh@gmail.com", "ff@gmail.com", "a@a.com", "f@f.com"], ["gg@gmail.com", "ff@gmail.com"]]

Any help would appreciate.

2 Answers2

1

Just grab all strings from both the array and remove the duplicates using Set

const parent = [["ff@gmail.com", "a@a.com", "f@f.com"], ["ff@gmail.com"]];
const customers = [["hh@gmail.com"], ["gg@gmail.com"]];

const result = parent.map((arr, i) => {
  return [...new Set([...customers[i], ...arr])];
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
DecPK
  • 24,537
  • 6
  • 26
  • 42
0

const parentArr = [['ff@gmail.com', 'a@a.com', 'f@f.com'], ['ff@gmail.com']];
let customersArr = [['hh@gmail.com'], ['gg@gmail.com']];
let newCustomersArr = [];

customersArr.forEach((customer, i) => {
    if (!parentArr[i].includes(customer[0])) {
        newCustomersArr.push(customer.concat(parentArr[i]));
    }
})

console.log(newCustomersArr);
Yoni A
  • 124
  • 8