0

我试图用 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"
    }
]
4

2 回答 2

0

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

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 数组的扫描时,它会使用arrayas中的已创建项目调用您的处理程序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; });
于 2015-07-05T11:58:59.157 回答
0

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

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();
于 2015-07-05T17:25:50.813 回答