有没有办法修改_.reject内部的原始数组?

我尝试了以下方法:

https://jsfiddle.net/jimmyt1988/LS384/953/

var section = {
    rows: [
    {name:"Alejandro", $initialized: true},
    {name:"Benito", $initialized: false},
    {name:"Chinea", $initialized: true},
    {name:"Domingo", $initialized: true},
    {name:"Eduardo", $initialized: false},
    {name:"Yolanda", $initialized: true},
    {name:"Zacarias", $initialized: true}
  ]
};

section.rows = _.reject(section.rows, function(row, index, sectionRows){
  if (!row.$initialized) {
    return true;
  }
  else
  {
    sectionRows[index] = { test: "test" };
    return false;
  }
});

console.dir(section.rows);

最佳答案

您的代码正在修改原始数组中的值,但是随后您用返回值_.reject覆盖了原始数组。删除section.rows =,您将看到修改后的值。

JSFiddle

关于javascript - 修改_.reject中的原始数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43870641/

10-11 12:22