【问题标题】:Nested linq loops嵌套的 linq 循环
【发布时间】:2015-07-05 11:44:23
【问题描述】:

我正在尝试使用 linq.js 迭代下面的 JSON,这里我的场景是我有一个包含 3、5、6 的数组。基于这个数组值,我需要来自 JSON 下面的 firstName。我怎样才能实现它。 JSON 仅作为实时处理 50000 条记录的示例,实现此目的的最佳方法是什么?

[
    {
        "id": "1",
        "firstName": "John",
        "lastName": "Doe"
    },
    {
        "id": "2",
        "firstName": "Anna",
        "lastName": "Smith"
    },
    {
        "id": "3",
        "firstName": "Peter",
        "lastName": "Jones"
    },
    {
        "id": "4",
        "firstName": "John",
        "lastName": "Doe"
    },
    {
        "id": "5",
        "firstName": "Anna",
        "lastName": "Smith"
    },
    {
        "id": "6",
        "firstName": "Peter",
        "lastName": "Jones"
    },
    {
        "id": "7",
        "firstName": "John",
        "lastName": "Doe"
    },
    {
        "id": "8",
        "firstName": "Anna",
        "lastName": "Smith"
    },
    {
        "id": "9",
        "firstName": "Peter",
        "lastName": "Jones"
    }
]

【问题讨论】:

    标签: javascript json node.js linq.js


    【解决方案1】:

    基本上,您要做的是加入。一种天真的方法只是简单地进行连接。

    var ids = [ 3, 5, 6 ];
    var query = Enumerable.From(data)
        .Join(ids, "Number($.id)", "$", "$")
        .ToArray();
    

    但是,当有许多对象要处理时,这不会很好地工作,因为您正在对相关项目执行线性搜索。您可以通过按 id 创建对象查找来改进事情。您只需支付创建查找的前期费用。

    var lookup = Enumerable.From(data)
        .ToObject("Number($.id)");
    var ids = [ 3, 5, 6 ];
    var query = Enumerable.From(ids)
        .Select(function (id) {
            return lookup[id];
        })
        .ToArray();
    

    【讨论】:

    • Jeff ,如果我想反转这个问题,例如我需要除 3、5、6 之外的所有数组值。我应该怎么做?
    【解决方案2】:

    如果你知道大量数据会挂起其他进程,让用户感到迟钝,你可以试试这个:

    var process = function(obj, targets, callback, context) {
        // Init context
        var results = [];
        var length = obj.length;
        var procLimit = 500; // Whatever the amount you want to process at a time, higher will have higher change to cause lag on browser.
        context = context ? context : null;
    
        var current = 0;
    
        // Loop function
        var looper = function() {
            if (current >= length) {
                callback.call(context, result);
                return;
            }
            var end = Math.min(length, current + procLimit);
            var id, findIndex;
            // Only process a fixed amount of item in a time.
            for (; current < end ; ++current) {
                id = parseInt(obj[current].id, 10);
                findIndex = targets.indexOf(id);
                // Find the matched key, and put to result.
                if (findIndex >= 0) {
                  results.push(obj[current]);
                  // or you just need fname, use
                  // results.push(obj[current].firstName);
                  // remove founded from targets
                  targets.splice(findIndex, 1);
                }
            }
            current += procLimit;
            // Non-blocking
            setTimeout(looper, 10);
        };
    
        // Non-blocking
        setTimeout(looper, 10);
    };
    // Usage
    process(YOUR_OBJ, [1, 3, 5, 6], function(processedObj){
      // do somthing with the processed Object
    }, THE_CONTEXT);
    

    此函数将尝试在后台运行,当它完成对 json 数组的扫描时,它会调用您的处理程序,并将 array 中的已创建项目作为 param

    如果不考虑块效应,则只需使用过滤器:

    var targets = [1, 3, 5, 6];
    var filterResult = YOUR_JSON.filter(function(item) {
      var id = parseInt(item.id, 10);
      return (targets.indexOf(id) >= 0);
    }).map(function(item) { return item.firstName; });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 2019-03-27
      • 2016-06-23
      • 2011-06-07
      • 2011-04-06
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多