下划线.js:替换集合中的项目

Underscore.js: Replace item in collection

本文关键字:项目 集合 替换 js 下划线      更新时间:2023-09-26

我已经使用下划线.js一个星期了,我真的很喜欢它。但是,现在我想用另一个元素替换集合 (vm.lists) 中的单个元素。我尝试执行以下操作:

_.each(vm.lists, function (el) {
    if (el.id == list.id)
        el = list;
});

和:

var l = _.findWhere(vm.lists, { id: list.id });
l = list;

但它们都不会改变集合中的实际项目。我该如何正确执行此操作?

你离得很近。问题是你只是替换指向该值的局部变量(ell),这不会修改初始列表。一个更简单的例子:

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的第一个选择会更干净。

这里不需要下划线:

for (var i = 0, l = vm.lists.length; i < l; i++)
{
  var el = vm.lists[i];
  if (el.id == list.id) {
    vm.lists[i] = list;
    break; // stop the for loop
  }
}