JavaScript map() is not saving the new elements?


Use the map() function correctly to save elements.

Example

Following is the code −

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);

To run the above program, you need to use the below command −

node fileName.js.

Here, my file name is demo323.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo323.js
[ [ 4, 5 ], [ 8, 57 ], [ 36, 80 ] ]

Updated on: 27-Oct-2020

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements