ahmadabdullah247
ahmadabdullah247

Reputation: 341

Map is not mapping

Hi I am trying to update my array using map function following this example:

var arr = ["1", 2, 3, 4];
arr = arr.map(function(v) {
  return "foo"+ v;
});

but in my code it is not updating, my code looks something like

obj['payment_info'].forEach(function(info) {
    info['method'].map(function(method) {
       return '';
    });
});

Upvotes: 2

Views: 481

Answers (1)

Adam Azad
Adam Azad

Reputation: 11297

The result of Array#map must be assigned to the old variable

obj['payment_info'].forEach(function(info) {
    info['method'] = info['method'].map(function(method) {
       return '';
    });
});

Here's a small test case with:

var arr = ["1", 2, 3, 4];
arr.map(function(v) {
  return "foo"+ v;
});
console.log(arr); // Output ["1", 2, 3, 4];

arr = arr.map(function(v) {
  return "foo"+ v;
});
console.log(arr); // Output ["foo1", "foo2", "foo3","foo4"];

Upvotes: 2

Related Questions