Home > other >  How to add two nested array in one nested array (In JavaScript)
How to add two nested array in one nested array (In JavaScript)

Time:10-13

I am having here two array which are:

const array1 = [[ 401, "John", "married"],[ 402, "Poul", "unmarried"]]

const array2 = [[ 401, 9654251, "[email protected]"],[ 402, 9623427, "[email protected]"]]

i want to add both array in one array somethings like:

array = [[401, "John", "married", 9654251, "[email protected]"],[ 402, "Poul", "unmarried", 9623427, "[email protected]"]]

How to Add two nested array in one nested array based on their index or common element's as id is here common elements,

In JavaScript.

anyone can help me to do this.

Thanks in Advance for your trying!

CodePudding user response:

You can use ES6 syntax to achieve with minimal code. Code may verry if your input and array element changes.

const array3 = array1.map((obj, i) => {
    return [...obj, ...array2[i].splice(1)]; // merge 2 array and create 1. also remove first element from second array
})

Output will be: [[401,"John","married",9654251,"[email protected]"],[402,"Poul","unmarried",9623427,"[email protected]"]]

  • Related