简体   繁体   English

underscore.js拒绝功能不起作用

[英]underscore.js reject function not working

Any idea why this piece of code is not working? 知道为什么这段代码不起作用吗? As you see, I am trying to delete the object with id = 1 from an array. 如您所见,我正在尝试从数组中删除id = 1的对象。

var arr = [{id: 1}, {id: 2}];
    console.log('arr: ', arr);

    _.reject(arr, function(obj) {
       console.log('Deleting obj')
      return obj.id == 1;
    });
console.log('arr now : ', arr); // This displays an unchanged array

You are not assigning back the result of _.reject to arr . 您没有将_.reject的结果分配回arr This will work: 这将起作用:

arr = _.reject(arr, function(obj) {
    console.log('Deleting obj')
    return obj.id == 1;
});

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

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