【问题标题】:Chaining methods on array display as undefined [duplicate]数组上的链接方法显示为未定义 [重复]
【发布时间】:2017-12-19 08:41:28
【问题描述】:
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.

但是,如果重新定义如下,它会按预期工作。

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

我想在一行中定义多维数组用作此状态命令。

但是对于有界 forEach 方法数组定义工作正常的场景 1 有什么问题?

【问题讨论】:

  • forEach 返回undefined
  • 如果您的意图是修改数组的内容,您可能需要使用Array#map,它会返回新数组developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 注意: Array.fill 将首先创建值并在所有项目中复制相同的值。所以在这段代码new Array(9).fill([])中,所有9个项目都将指向同一个数组
  • @apokryfos 按照规范ecma-international.org/ecma-262/6.0/… ...它返回undefined ...几乎相当于没有return声明。
  • 使用.map 做一行。同意@user184994 !!

标签: javascript arrays method-chaining


【解决方案1】:

但是对于有界 forEach 方法的场景 1 的问题是什么 数组定义工作正常。

问题是forEach 返回undefined

我想在一行中定义多维数组 用作 this.state 命令。

你也可以使用map

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

或者像@bergi建议的那样,你也可以使用Array.from

var output = Array.from(Array(9)).map( function(value,index,arr){
   return Array(9).fill(0); 
});

【讨论】:

  • 或者用fill+map代替Array.from
  • @Bergi 这就是(在我的回答中编辑)你的意思吗?
  • @gurvinder372 不,我的意思是没有map,就像在 Rajesh 的评论中一样。
  • @Rajesh 请添加 yours 作为答案,它更时尚更短。 +1
  • 谢谢大家,forEach 的解释描述了根本原因。使用 map 或 from 作为替代方案是个好主意。而且在Javascrpt中初始化多维数组也没有其他语言方便。
【解决方案2】:

正如其他答案已经提到的Array.forEach返回undefined或不返回任何东西,你不能使用它。

作为替代方法,您可以使用Array.from

以下是示例:

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

console.log(output)

另外,您的代码还有一个问题是,您正在使用Array.fill 中的一个对象。 array.fill 所做的是,它会先创建要填充的值,然后将其填充到所有项目中。

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

在您检查输出时,它显示 /**ref:2**/,但是如果您在控制台中检查,您会注意到所有项目都将具有项目 1。所以你应该避免使用带有 Array.fill 的对象。

【讨论】:

    【解决方案3】:

    你想生成一个由 0 填充的 9 个数组组成的数组并将其返回给变量。

    您可以使用map 方法实现此目的。

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

    附言在您的情况下,不需要传递给 map 方法参数的值、索引等

    【讨论】:

      【解决方案4】:

      Array.prototype.forEach 返回undefined。只需拨打forEach,如下所示。

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

      使用map 将是另一种选择,但我不投赞成票,因为它会在内存中创建另一个数组。

      【讨论】:

        猜你喜欢
        • 2013-03-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 2021-04-08
        • 1970-01-01
        • 2020-08-23
        • 1970-01-01
        • 2022-12-04
        相关资源
        最近更新 更多