Sử dụng đúng hàm map () để lưu các phần tử.
Ví dụ
Sau đây là mã -
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 chương trình trên, bạn cần sử dụng lệnh dưới đây -
node fileName.js.
Đây, tên tệp của tôi là demo323.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau -
PS C:\Users\Amit\javascript-code> node demo323.js [ [ 4, 5 ], [ 8, 57 ], [ 36, 80 ] ]