使用关联键更新JSON值

时间:2014-09-26 09:33:54

标签: javascript jquery json

我正在尝试收集唯一的json数据,我的意思是如果密钥存在更新其值。但是没有成功更新现有密钥的值。

var fpr_data = [{"rfr_id":"7","user_id":"5","fp_id":"10","raw_id":"3","raw_qty":"20.00","raw_wastage":"2","raw_name":"Buttons"}, 
                {"rfr_id":"9","user_id":"5","fp_id":"10","raw_id":"4","raw_qty":"500.00","raw_wastage":"0","raw_name":"Yarn"}, 
                {"rfr_id":"8","user_id":"5","fp_id":"10","raw_id":"5","raw_qty":"2.00","raw_wastage":"1","raw_name":"Needle"},  
                {"rfr_id":"7","user_id":"5","fp_id":"10","raw_id":"3","raw_qty":"20.00","raw_wastage":"2","raw_name":"Buttons"}]; 

var qty = 2, coll={}, _qty=0.00,_wastage=0.00;

// Filter and modify JSON data
$.each(fpr_data, function(i, data) {
    _qty = data.raw_qty * qty;
    _wastage = data.raw_wastage * qty;

    // Next time add on existing keys
    if( coll[data.raw_id] == data.raw_id ) {
        var q = coll[data.raw_id].qty + _qty;
        var w = coll[data.raw_id].wastage + _wastage;

        coll[data.raw_id] = {"qty":q, "wastage":w};
    }
    else {
        coll[data.raw_id] = {"qty":_qty, "wastage":_wastage};
    }
});
console.log(coll);

fpr_data中有raw_id我想要收集唯一ID,如果在对象中找到raw_id,则用qty更新其wastageraw_qtyraw_wastage。我获得了唯一的JSON数据,但数量和浪费没有得到更新。 我做错了什么?您可以在小提琴中找到相同的代码并在控制台中查看结果。

预期 qty3的值应为80

JSFIDDLE

2 个答案:

答案 0 :(得分:2)

如果对象已存在于数组中,则以下条件不会给出正确的比较。

  

if(coll [data.raw_id] == data.raw_id){

我认为你应该这样做:

  

if(coll [data.raw_id]){

如果我理解你正确尝试此example

if(coll[data.raw_id]) {

            var q = coll[data.raw_id].qty + _qty;
            var w = coll[data.raw_id].wastage + _wastage;

            coll[data.raw_id] = {"qty":q, "wastage":w};
        }
        else {
            coll[data.raw_id] = {"qty":_qty, "wastage":_wastage};
        }

答案 1 :(得分:0)

您使用jQuery,所以享受jQuery.extend()函数:

var fpr_data = [{"rfr_id":"7","user_id":"5","fp_id":"10","raw_id":"3","raw_qty":"20.00","raw_wastage":"2","raw_name":"Buttons"},{"rfr_id":"9","user_id":"5","fp_id":"10","raw_id":"4","raw_qty":"500.00","raw_wastage":"0","raw_name":"Yarn"},{"rfr_id":"8","user_id":"5","fp_id":"10","raw_id":"5","raw_qty":"2.00","raw_wastage":"1","raw_name":"Needle"}, {"rfr_id":"7","user_id":"5","fp_id":"10","raw_id":"3","raw_qty":"20.00","raw_wastage":"2","raw_name":"Buttons"}];
console.log(fpr_data);
var qty = 2, coll={}, _qty=0.00,_wastage=0.00;

    // Filter and modify JSON data
    $.each(fpr_data, function(i, data) {
        _qty = data.raw_qty * qty;
        _wastage = data.raw_wastage * qty;

        // Next time add on existing keys
        var currentObj = coll[data.raw_id]; // Try not to repeat yourself ;-)
        if( currentObj == data.raw_id ) {
            var q = currentObj.qty + _qty;
            var w = currentObj.wastage + _wastage;
            console.log(data);
            coll[data.raw_id] = $.extend(data, {"qty":q, "wastage":w});
        }
        else {
            coll[data.raw_id] = $.extend(data, {"qty":_qty, "wastage":_wastage});
        }
    });
console.log(coll);

我希望这就是你要找的东西。