修改_.reject中的原始数组

时间:2017-05-09 13:04:24

标签: javascript underscore.js

有没有办法修改_.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);

1 个答案:

答案 0 :(得分:1)

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

JSFiddle