繁体   English   中英

从列表Node.JS中删除项目

[英]Removing item from list Node.JS

如果item.profit小于1,是否有人知道如何从下面的代码list中删除item

接头似乎无效。

假设这返回的内容如下:

item 1, profit = 10
item 2, profit = 5
item 3, profit = -3
item 4, profit = 5
item 5, profit = -2

我试图以列表结尾:

item 1, profit = 10
item 2, profit = 5
item 3, profit = 5

var i = 0;
async.forEach(list, function (item, callback) {

    var url = 'http://www.website.com';
    request(url, function (err, response, html) {
        if (err) {
            console.log(err)
        } else {
            /// some code before....

            item.profit = (some calculation here);

            if (item.profit < 1) {
                console.log("Profit less than 1, so removing from list...");
                list.splice(i, 1); // This doesn't seem to work
            }

            // some code after...

        }
        callback();
    });
}, function (err) { //This is the final callback
    callback(err, list);
});

谢谢,

安东尼

请注意,async.forEach()并行处理项目。 因此,修改原始列表的条目是一个问题。 您可能需要将所有需要的条目保存在新列表中。 这是代码:

var i = 0, 
    newList = [];
async.eachSeries(list, function (item, callback) {  // <<<<< done is series to keep
                                                    // the same order as the original
                                                    // list, if you don't care about
                                                    // the order, then use
                                                    // async.each()

  var url = 'http://www.website.com';
  request(url, function (err, response, html) {
    if (err) {
        console.log(err)
    } else {
        /// some code before....

        item.profit = (some calculation here);

        if (item.profit > 0) {
            newList.push(item);
        }

        // some code after...

    }
    callback();
  });
}, function (err) { //This is the final callback
  callback(err, newList);
});

暂无
暂无

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

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