0
const array = new Array(9).fill([]).forEach(function(value, index, arr) {
    arr[index] = Array(9).fill(0);
    console.log(index, arr[index]); // This line report properly by creating 
}); // new array.
console.log(array); // Reported as underdefined.

However, if redefine as below, it works as expected.

const array = new Array(9).fill([]);
array.forEach( function(value,index,arr){
   arr[index] = Array(9).fill(0);
   console.log(index,arr[index]);   
});                                  

console.log(array);    

I would like to define multiple dimensional arrays within one line used as this state command.

But what is the problem for scenario 1 where bounded forEach methods array definitions work fine?

Adinia
  • 3,722
  • 5
  • 40
  • 58
Il Kim
  • 3
  • 1
  • 6
    `forEach` returns `undefined` – gurvinder372 Dec 19 '17 at 08:42
  • 3
    If your intent is to modify the contents of the array, you may want to use `Array#map` instead, which returns the new array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map – user184994 Dec 19 '17 at 08:43
  • 1
    **Note:** Array.fill will create the value first and copy same value in all items. So in this code `new Array(9).fill([])`, all 9 items will point to same array – Rajesh Dec 19 '17 at 08:45
  • @gurvinder372 bit of a technical question. Does foreach return `undefined` or does foreach not return anyting and therefore assigning nothing to a variable is undefined? – apokryfos Dec 19 '17 at 08:45
  • @apokryfos this may be a stupid question but is `return undefined` different from not returning? – Rajesh Dec 19 '17 at 08:46
  • 1
    @apokryfos as per spec http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.foreach ... it returns `undefined` ... And it is almost equivalent to not having a `return` statement. – gurvinder372 Dec 19 '17 at 08:47
  • 1
    use `.map` to do in one line.agreed with @user184994 !! – Muneer Alam Dec 19 '17 at 08:51
  • @Rajesh I don't think there's any practical difference. But I would expect a difference in how you express intent when writing code i.e. if someone else sees your code and sees `return undefined;` then they'd reasonably assume that they can work with that but if you don't return anything they can reasonably assume that they should not work with any return value – apokryfos Dec 19 '17 at 08:51

4 Answers4

4

But what is the problem for scenario 1 where bounded forEach methods array defnitions works fine.

Problem is forEach returns undefined

I woule like to define multiple dimensional arrays within one line used as this.state command.

You can use map for the same

var output = new Array(9).fill([]).map( function(value,index,arr){
   return Array(9).fill(0);  //return the inner array
});

Or as @bergi suggested, you can use Array.from as well

var output = Array.from(Array(9)).map( function(value,index,arr){
   return Array(9).fill(0); 
});
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
1

As other answers have already mentioned Array.forEach returns undefined or does not return anything, you cannot use it.

As an alternate approach, you can use Array.from.

Following is a sample:

var output = Array.from({
  length: 9
}, () => {
  return Array.from({
    length: 9
  }, () => 0)
});

console.log(output)

Also, one more issue with your code is, you are using an object in Array.fill. What array.fill does is, it will create the value to be filled first and then fill it in all items.

var output = new Array(9).fill([]);
output[0].push(1);
console.log(output)

In you check the output, it says /**ref:2**/, but if you check in console, you will notice that all items will have item 1. So you should avoid using objects with Array.fill.

Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

You want to generate array of 9 arrays filled with 0 and return it to variable.

You can achieve this using map method.

const array = 
  new Array(9)
      .fill([])
      .map(function(){
        return Array(9).fill(0);
       });     
       
console.log(array);   

p.s. no value, index and etc needed to pass to map methods argument in Your case

num8er
  • 18,604
  • 3
  • 43
  • 57
0

Array.prototype.forEach returns undefined. Just call the forEach like below.

const array = new Array(9).fill([])
array.forEach( function(value,index,arr){
   arr[index] = Array(9).fill(0);
});
// [
//   [0, 0, 0, ...],
//   ...
// ]

Using map would be another choice, but I don't vote for it because it creates yet another array in memory.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474