如果我有以下JSON,

[{},{"param":"#content","value":"K2-12M","quantity":1,"q_id":3,"clear":1}
{"param":"#content","value":"K2-12F","quantity":2,"q_id":3,"clear":0}]


在js / jquery中,我将如何遍历,如果任何项目具有"clear":0,然后将所有项目设置为"clear":0

最佳答案

var clear;
for( var i=0, l=json.length; i<l; i++ ){
    if( 0 === json[i].clear ){
        clear = true;
        break;
    }
}

if( clear ){
    for( i=0; i<l; i++) {
        json[i].clear = 0;
    }
}




或使用jQuery(效率较低):

$( json ).filter(
    function( ix, obj ){
        return 0 === obj.clear;
    }
).length
    && $( json ).each(
        function( ix, obj ){
            obj.clear = 0;
        }
    );

关于javascript - Javascript循环json获取值,并在满足条件时应用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7600505/

10-10 00:38
查看更多