【问题标题】:Underscore.js: Replace item in collectionUnderscore.js:替换集合中的项目
【发布时间】:2015-01-17 09:42:26
【问题描述】:

我已经使用 underscore.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;

但它们都没有改变集合中的实际项目。我该如何正确地做到这一点?

【问题讨论】:

    标签: javascript arrays collections underscore.js


    【解决方案1】:

    你已经很接近了。问题是您只替换了指向该值的局部变量(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 第一个选项会更干净。

    【讨论】:

      【解决方案2】:

      这里不需要下划线:

      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
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-20
        • 2016-02-20
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 1970-01-01
        • 2016-08-17
        相关资源
        最近更新 更多