Roni
Roni

Reputation: 25

forEach returns undefined - How to return an array instead?

Given below is the code in which I tried to modify an array using arrow function but while running, it returns a value of undefined as the output can someone give suggestions please

var modifyArray = (nums) => {
     return nums.forEach(function (element) {
          (element % 2 == 0) ? element *= 2 : element *= 3;
     })
}
console.log(modifyArray([1,2,3,4,5]));

Upvotes: 0

Views: 1585

Answers (5)

Jonas Wilms
Jonas Wilms

Reputation: 138407

.forEach returns undefined. You probably want to .map to a new array and return that:

 return nums.map(function (element) {
      return element * ((element % 2 == 0) ? 2 : 3);
 });

For sure you also have to return the statement inside of the inner function, otherwise that evaluates to undefined too.

Upvotes: 3

weegee
weegee

Reputation: 3399

For each returns undefined as it doesn't return anything but you can use forEach for doing the same thing by pushing your data into an array. Notice I did not change your approach but just extended it

var modifyArray = (nums) => {
  var somearray = []
  nums.forEach((element) => somearray.push((element % 2 == 0) ? element *= 2 : element *= 3));
  return somearray;
}
console.log(modifyArray([1, 2, 3, 4, 5]));

Use array.push to push data to an array. If you want to use the map method you can also use that like

var modifyArray = (nums) => {
     return nums.map((element) => { return (element % 2 == 0) ? element *= 2 : element *= 3 });
}
console.log(modifyArray([1,2,3,4,5]));

Wanna use for loops? Then you can also try this approach. It's the same as the forEach loop

var modifyArray = (nums) => {
  var somearray = [];
  for (let i = 0; i < nums.length; i++) {
    somearray.push((nums[i] % 2 == 0) ? nums[i] *= 2 : nums[i] *= 3)
  }
  return somearray;
}

console.log(modifyArray([1, 2, 3, 4, 5]));

Upvotes: 0

balzacLeGeek
balzacLeGeek

Reputation: 815

What about using for loops?

var modifyArray = (nums) => {
    for (let i = 0; i < nums.length; i++) {
        let element = nums[i];
        // console.log(element + ' - ' + element % 2 );
        nums[i] = (element % 2 == 0) ? element *= 2 : element *= 3;
    }
    return nums;
}

console.log(modifyArray([1,2,3,4,5]));

Upvotes: 0

M K
M K

Reputation: 306

See the answers above.

There is a workaround if you still want to use forEach

let result = []
var modifyArray = (nums) => {
     nums.forEach(function (element) {
          result.push((element % 2 == 0) ? element *= 2 : element *= 3);
     })
    return result;

}
modifyArray([1,2,3,4,5])
console.log(result)

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44125

forEach doesn't return anything - use map instead:

var modifyArray = nums => nums.map(element => element % 2 == 0 ? element *= 2 : element *= 3)
console.log(modifyArray([1,2,3,4,5]));

Also note that inside of the function passed to forEach you weren't returning anything - with ES5 function you need to explicitly return - not required with ES6 =>.

Upvotes: 0

Related Questions