循环数组成员,并更新

时间:2014-09-26 12:32:45

标签: jquery loops

不,我有一些阵列客户

和一些值

[{
    "id": 1,
    "name": "John",
    "score": 4300,
    "active": 1,
}, {
    "id": 2,
    "name": "Ari",
    "score": 6000,
    "active": 1
}, {
    "id": 3,
    "name": "Jared",
    "score": 999999,
    "active": 0
}, {
    "id": 4,
    "name": "Mike",
    "score": 100,
    "active": 0
}]

还有一些值

var id = 'Mike';
var score = '100';
var active ='1';

如何循环客户并替换当前值

2 个答案:

答案 0 :(得分:0)

使用jQuery.each()循环播放数组:

$.each(arrayname, function(index, value) {
    //do your processing
});

答案 1 :(得分:0)

试试这个:

        var customers = [{
            "id": 1,
            "name": "John",
            "score": 4300,
            "active": 1,
        }, {
            "id": 2,
            "name": "Ari",
            "score": 6000,
            "active": 1
        }, {
            "id": 3,
            "name": "Jared",
            "score": 999999,
            "active": 0
        }, {
            "id": 4,
            "name": "Mike",
            "score": 100,
            "active": 0
        }];

        var name = 'Mike';
        var score = '100';
        var active ='1';
        var temp_cust = "";
        for(var i=0; i<customers.length; i++){
            if(customers[i]["id"]==4){
                customers[i]["active"]=active;
            }
            //alert(customers[i]["active"] +" for "+customers[i]["name"]); //for test
        }
        console.log(customers); //log in console