简体   繁体   English

修改_.reject中的原始数组

[英]Modify the original array inside _.reject

Is there a way to modify the original array inside an _.reject? 有没有办法修改_.reject内部的原始数组?

I've tried the following: 我尝试了以下方法:

https://jsfiddle.net/jimmyt1988/LS384/953/ 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);

Your code is modifying the values in the original array, but then you overwrite the original array with the return value of _.reject . 您的代码正在修改原始数组中的值,但随后您使用_.reject的返回值覆盖了原始数组。 Remove the section.rows = and you'll see the modified values. 删除section.rows = ,您将看到修改后的值。

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM