通过id将json数组合成一个json数组

时间:2015-04-09 03:18:03

标签: javascript jquery arrays json

我想通过匹配其属性值来合并项目并购买json数组。

以下是来源:

{
    "item": [
        {
            "invoiceId": 1
        },
        {
            "invoiceId": 2
        },
        {
            "invoiceId": 3
        }
    ],
    "purchase": [
        {
            "id": "1",
            "date": "12/1/2014"
        },
        {
            "id": "2",
            "date": "12/1/2014"
        },
        {
            "id": "3",
            "date": "12/1/2014"
        }
    ]
}

我想生产这样的东西:

{
    "combined": [
        {
            "invoiceId": 1,
            "id": "1",
            "date": "12/1/2014"
        },
        {
            "invoiceId": 2,
            "id": "2",
            "date": "12/1/2014"
        },
        {
            "invoiceId": 3,
            "id": "3",
            "date": "12/1/2014"
        }
    ]
}

如何将item.invoiceId与purchase.id匹配?

3 个答案:

答案 0 :(得分:2)

解决方案

假设obj是您的对象

var new_obj = {combined:[]};
obj["purchase"].forEach(function(a) {
    obj["item"].forEach(function(b){ 
        if (+b["invoiceId"]===(+a["id"])) {
            a["invoiceId"] = b["invoiceId"] || 0;//WILL MAKE INVOICEID 0 IF IT IS NOT DEFINE. CHANGE 0 TO YOUR NEEDS
            new_obj.combined.push(a);
        }
    });
});

如何运作

第一个.forEach()循环遍历obj.purchase。然后我们循环浏览obj.item要检查它们是否匹配invoiceId (如果您不需要确保它们是匹配的invoiceId,请使用备用代码。然后,我们只需向new_obj

添加一个新值

结果(从控制台复制)是:

{
    "combined":[
        {
            "id":"1",
            "date":"12/1/2014",
            "invoiceId":1
        },
        {
            "id":"2",
            "date":"12/1/2014",
            "invoiceId":2
        },
        {
            "id":"3",
            "date":"12/1/2014",
            "invoiceId":3
        }
    ]
} 

替代代码

如果您不需要确认invoiceId

,请使用此选项
var new_obj = {combined:[]};

obj["purchase"].forEach(function(a){a["invoiceId"]=a["id"];new_obj.combined.push(a);});

答案 1 :(得分:0)

实现目标的一种方法是

var result = {};

var getMatchingPurchase = function(invoiceId) {
    return data.purchase.filter(function(purchase) {
        return invoiceId == purchase.id; 
    })[0];
};

result.combined = data.item.map(function(invoice) {
    var purchase = getMatchingPurchase(invoice.invoiceId);
    return {
        invoiceId: invoice.invoiceId,
        id: purchase.id,
        date: purchase.date
    };
});

console.log(result);

它会像下面那样打印

{ combined:
   [ { invoiceId: 1, id: '1', date: '12/1/2014' },
     { invoiceId: 2, id: '2', date: '12/1/2014' },
     { invoiceId: 3, id: '3', date: '12/1/2014' } ] }

注意: - 我使用IE8不支持的地图过滤器功能。如果要在IE8中使用,则必须使用 for循环。

答案 2 :(得分:0)

如果你必须支持像IE8这样的老浏览器(穷人......),请注意可能不支持本机forEach,在这种情况下,你可以使用lodash来实现跨浏览器的兼容性:

function getCombinedResult(source){
    var combinedList = [];
    _.each(source.item, function(item){
        _.each(source.purchase, function(purchase){
            if (item['invoiceId'].toString() != purchase['id'].toString()) return;
            var combinedItem = _.extend(item, purchase)
            combinedList.push(combinedItem);
        });
    })
    return {"combined": combinedList};
}