The map loop is nested, always getting the last value of the outer array

there are two arrays arr01 and arr02
now add a field from the arr01 array to the arr02. The problem now is that the resulting new array is always the last field of the arr01

let arr01 = [
    {
        "account" : "000000000000"
    },
    {
        "account" : "111111111111"
    },
    {
        "account" : "2222222222222"
    }
];
let arr02 = [
    {
        "city" : ""
    },
    {
        "city" : ""
    },
    {
        "city" : ""
    }
];
                
let newArr = [];
arr01.map((cur01,eq) => {
    arr01.map((cur02,index) => {
        cur02.account = cur01.account;
        return newArr.push(cur);
    })
})

final result:

newArr = [
    {
        "account" : "2222222222222",
         "city" : ""
    },
    {
        "account" : "2222222222222",
        "city" : ""
    },
    {
        "account" : "2222222222222",
        "city" : ""
    },
    {
        "account" : "2222222222222",
         "city" : ""
    },
    {
        "account" : "2222222222222",
        "city" : ""
    },
    {
        "account" : "2222222222222",
        "city" : ""
    },
    {
        "account" : "2222222222222",
         "city" : ""
    },
    {
        "account" : "2222222222222",
        "city" : ""
    },
    {
        "account" : "2222222222222",
        "city" : ""
    }
];

the result I want is:

newArr = [
    {
        "account" : "000000000000",
         "city" : ""
    },
    {
        "account" : "000000000000",
        "city" : ""
    },
    {
        "account" : "000000000000",
        "city" : ""
    },
    {
        "account" : "111111111111",
         "city" : ""
    },
    {
        "account" : "111111111111",
        "city" : ""
    },
    {
        "account" : "111111111111",
        "city" : ""
    },
    {
        "account" : "2222222222222",
         "city" : ""
    },
    {
        "account" : "2222222222222",
        "city" : ""
    },
    {
        "account" : "2222222222222",
        "city" : ""
    }
];
Mar.09,2021

dude, if you ask questions, you should type the code well, otherwise it looks very messy and you are not in the mood to answer questions. I've already lined up for you.

< hr >

look at your problem, it should be to merge the two arrays into one array according to the sequence number. Your method is problematic, because each time you loop arr01 to the last element, and then change the data in arr02 .

The

map function returns a new array, so instead of nesting map , just loop and reassemble the array.

refer to this:

var arr = [];
arr01.forEach(function(value, index) {
    //  arr02  city
    value.city = arr02[index] || '';
    arr.push(value)
})
console.log(arr)

or:

var arr = [];
arr02.forEach(function(value, index) {
    //  arr01  acdount
    value.account = arr01[index] || '';
    arr.push(value)
})
console.log(arr)
< hr >

if you modify the problem, you want each to be modified accordingly, so nest the loop twice. Instead of using map , you can directly use forEach .

var arr = [];
arr01.forEach(function(v, i) {
    arr02.forEach(function(vv, ii) {
        arr.push({
            account: v.account,
            city: vv.city
        })
    })
})
console.log(arr)

what you have changed is the reference address of the object. All the effects of the last change have been entered by the previous push

.
var newArr = [];
arr01.forEach(v1 => {
    arr02.forEach(v2 => {
        newArr.push(Object.assign({}, v2, {
            account: v1.account
        }))
    })
})
console.log(newArr)

The return value of

map is an array. For this problem, forEach is more appropriate

.
let newArr = [];
arr01.forEach(a1 => {
    arr02.forEach(a2 => {
        newArr.push({
            account: a1.account,
            city: a2.city
        });
    });
});

first of all, there is nothing wrong with your thinking, but you use

cur02.account = cur01.account;
return newArr.push(cur02);

in the above code, you push is cur02 , and js object assignments are passed reference , so the result at the end of the loop is:

// [arr02[0], arr02[1], arr02[2], arr02[0], arr02[1], arr02[2], arr02[0], arr02[1], arr02[2]]

in your last loop, the account value of each object in arr02 becomes "222222222222222" .

so, when you push , you push a new object goes in, instead of a reference to an existing object can solve the problem. In addition, map is changed to forEach ( map and forEach have different usage scenarios).

let newArr = [];
arr01.forEach(cur01 => {
    arr02.forEach(cur02 => {
        newArr.push({
            account: cur01.account,
            city: cur02.city
        });
    })
})
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b254-2b9ea.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b254-2b9ea.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?