Sử dụng map() đúng cách để lưu phần tử mới.
Ví dụ
Mã thực hiện:
const addIndexValueToArrayElement = function (arrObject) {
const updatedArrayValue = [];
arrObject.forEach(function (ob) {
const mapValue = ob.map(function (value) {
return value + arrObject.indexOf(ob)
})
updatedArrayValue.push(mapValue)
})
return updatedArrayValue;
};
const output = addIndexValueToArrayElement([
[4, 5],
[7, 56],
[34, 78],
]);
console.log(output);
Chạy lệnh:
node fileName.js.
Tên file: demo323.js.
Kết quả
Đầu ra:
PS C:\Users\Amit\javascript-code> node demo323.js [ [ 4, 5 ], [ 8, 57 ], [ 36, 80 ] ]