你已经很接近了。问题是您只替换了指向该值的局部变量(el 和l),这不会修改初始列表。一个更简单的例子:
var list = [1, 2, 3];
var v = list[1];
v = 7; // v will now point to a new value, but list will stay intact [1, 2, 3]
对象也一样:
var olist = [{id: 1, v: 2}, {id: 4, v: 6}];
var obj = olist[0];
obj = {id: 8, v: 10}; // changes what obj points to, but does not affect olist[0]
var obj2 = olist[0];
obj2.v = 777; // olist[0] still points to the same object but the object is modified
olist[0] = {id: 8, v: 10}; // changes what olist[0] points to
所以基本上你有两个选择:
a) 更改 vm.lists[index] 使其指向一个新对象。您需要获取列表中对象的索引并执行vm.lists[index] = newObject;。请注意,一些下划线谓词还为您提供索引_.each(list, function (el, index) {});。
b) 更改vm.lists[index] 指向的对象,但您需要手动复制字段。例如,如果您的对象表示为 {id: id, values: [1, 2, 3], anotherField: data},您可以复制字段:
//el.id = list.id; // don't need this as you were searching by id
el.values = list.values;
el.anotherField = list.anotherField;
IMO 第一个选项会更干净。