Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “使用”有什么不对;forEach“是什么;?_Javascript - Fatal编程技术网

Javascript “使用”有什么不对;forEach“是什么;?

Javascript “使用”有什么不对;forEach“是什么;?,javascript,Javascript,我希望errorLogArrayList.length为3,但结果为2 谁能告诉我为什么这个代码不能像我期望的那样工作,以及如何修复它 代码: var logArrayList = [ [1,2,3], [1,2,"error"], [1,2,"error"], [1,2,"error"] ] var errorLogArrayList = []; console.log(logArrayList); console.log("================

我希望
errorLogArrayList.length
为3,但结果为2

谁能告诉我为什么这个代码不能像我期望的那样工作,以及如何修复它

代码:

var logArrayList = [
    [1,2,3],
    [1,2,"error"],
    [1,2,"error"],
    [1,2,"error"]
]

var errorLogArrayList = [];

console.log(logArrayList);
console.log("======================");


logArrayList.forEach(function(logArray, index, self) {

    if(logArray[2] === "error") {

        var errorArray = self.splice(index, 1)[0];
        errorLogArrayList.push(errorArray);

    }

}); 

// I expect this value to be 3, but the result is 2.
console.log("errorLogArrayList.length is " +  errorLogArrayList.length)

console.log(errorLogArrayList);

console.log("======================");

// I expect this value to be 1, but the result is 2.
console.log("logArrayList.length is " + logArrayList.length);
console.log(logArrayList);
[ [ 1, 2, 3 ],
  [ 1, 2, 'error' ],
  [ 1, 2, 'error' ],
  [ 1, 2, 'error' ] ]
======================
// I expect this value to be 3, but the result is 2.
errorLogArrayList.length is 2
[ [ 1, 2, 'error' ], [ 1, 2, 'error' ] ]
======================
//I expect this value to be 1, but the result is 2.
logArrayList.length is 2
[ [ 1, 2, 3 ], [ 1, 2, 'error' ] ]
日志:

var logArrayList = [
    [1,2,3],
    [1,2,"error"],
    [1,2,"error"],
    [1,2,"error"]
]

var errorLogArrayList = [];

console.log(logArrayList);
console.log("======================");


logArrayList.forEach(function(logArray, index, self) {

    if(logArray[2] === "error") {

        var errorArray = self.splice(index, 1)[0];
        errorLogArrayList.push(errorArray);

    }

}); 

// I expect this value to be 3, but the result is 2.
console.log("errorLogArrayList.length is " +  errorLogArrayList.length)

console.log(errorLogArrayList);

console.log("======================");

// I expect this value to be 1, but the result is 2.
console.log("logArrayList.length is " + logArrayList.length);
console.log(logArrayList);
[ [ 1, 2, 3 ],
  [ 1, 2, 'error' ],
  [ 1, 2, 'error' ],
  [ 1, 2, 'error' ] ]
======================
// I expect this value to be 3, but the result is 2.
errorLogArrayList.length is 2
[ [ 1, 2, 'error' ], [ 1, 2, 'error' ] ]
======================
//I expect this value to be 1, but the result is 2.
logArrayList.length is 2
[ [ 1, 2, 3 ], [ 1, 2, 'error' ] ]
您只需执行以下操作:

var errorLogArrayList = logArrayList.filter(function(array) {
  return array[2] === 'error';
});

logArrayList = logArrayList.filter(function(array) {
  return array[2] !== 'error';
});
不幸的是,这需要一些重复的迭代。如果需要,您可以使用(看起来更干净一点):

当前方法的问题是,您试图跟踪多个状态并同时修改两个数组,这会导致一些数据冲突
filter
通常更具声明性,它不会改变原始数组,而是返回一个新数组

编辑 想要添加一个带有
reduce
的方法,如@zerkms在评论中所建议的:

var lists = logArrayList.reduce(function(agg, curr) {
  if(curr[2] === 'error') {
    agg[0] = (agg[0] || []).concat([curr]);
  } else {
    agg[1] = (agg[1] || []).concat([curr]);
  }

  return agg;
}, []);
这与
分区
示例相同。

您只需执行一个操作:

var errorLogArrayList = logArrayList.filter(function(array) {
  return array[2] === 'error';
});

logArrayList = logArrayList.filter(function(array) {
  return array[2] !== 'error';
});
不幸的是,这需要一些重复的迭代。如果需要,您可以使用(看起来更干净一点):

当前方法的问题是,您试图跟踪多个状态并同时修改两个数组,这会导致一些数据冲突
filter
通常更具声明性,它不会改变原始数组,而是返回一个新数组

编辑 想要添加一个带有
reduce
的方法,如@zerkms在评论中所建议的:

var lists = logArrayList.reduce(function(agg, curr) {
  if(curr[2] === 'error') {
    agg[0] = (agg[0] || []).concat([curr]);
  } else {
    agg[1] = (agg[1] || []).concat([curr]);
  }

  return agg;
}, []);

这与
分区
示例相同。

不要修改您正在迭代的数组。@zerkms那么,我应该做的是克隆原始数组并执行其余操作?@zenkms如果我只修改一个数组,而不是同时修改两个数组,是否可以修改一个数组?不要修改您正在迭代的数组。@zerkms so,我应该做的是克隆原始数组,然后再做其余的事情?@zenkms如果我只修改一个数组,而不是同时修改两个数组,那么修改一个数组可以吗?。。。或者
Array.prototype.reduce
谢谢!我现在要试试看!。。。或者
Array.prototype.reduce
谢谢!我现在就去试试!