如何通过属性更新数组中的对象

时间:2019-01-31 07:34:37

标签: javascript json

如果LabelTime等于当前时间,我想按网关名称对网关对象进行计数,但是当我更新计数器时,它将更新所有对象。

这是示例数据。

[{label:"00", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"01", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"02", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"03", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"04", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"05", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"06", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"07", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"08", count:20, gateway:100}, {label:"09", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"10", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"11", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"12", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"13", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"14", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"15", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"16", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"17", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"18", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"19", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"20", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"21", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"22", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}, {label:"23", count:0, gateway:{'smsc-0':0, 'smsc-1':0, 'smsc-2':0}}]

代码

function time_count($date_array)
{
    var initTime = [];

    for (var i = 0; i < 24; i++) 
    {
        initTime.push({
            'label': String(i).padStart(2, '0'),
            'count': 0,
            'gateway': GlobalVariables.gateway,
        }); 
    }
    if ($date_array != null)
    {   
        $.each($date_array, function(iDate,vDate){
            var today = new Date();
            today = today.getFullYear()+"-"+(String(today.getMonth() + 1).padStart(2,'0'))+"-"+String(today.getDate()).padStart(2,'0');
            var new_date = new Date(vDate['DateTime']);
            var vdate = new_date.getFullYear()+"-"+(String(new_date.getMonth() + 1).padStart(2,'0'))+"-"+String(new_date.getDate()).padStart(2,'0');

            if(today == vdate)
            {
                $.each(initTime, function(iTime,vTime){

                    if(vTime.label == new_date.getHours()){
                        var current = parseInt(vTime.count) + 1;
                        vTime.count = current;  
                        vTime.gateway[vDate['SMSC']] = parseInt(vTime.gateway[vDate['SMSC']]) + 1;;

                    }
                });
            }
        });
    }

    return initTime;
}

1 个答案:

答案 0 :(得分:0)

您将每个ititTime项目的网关属性设置为相同的对象引用,然后使用以下方法对该对象进行突变:vTime.gateway[vDate['SMSC']] =

您为每个initTime项目设置了“ GlobalVariables.gateway”的浅表副本。

    initTime.push({
        'label': String(i).padStart(2, '0'),
        'count': 0,
        'gateway': Object.assign({},GlobalVariables.gateway),
    });