过滤JSON

时间:2013-03-05 11:12:18

标签: javascript json

我正在使用下面的JSON

{
    "status": {
        "On Hold": true,
        "Completed": true,
        "Work In Progress": false,
        "Closed": false,
        "Initiation": false
    },
    "application": {
        "Athena":false,
        "EWindow":false,
        "EWS":true,
        "FACT":false,
        "FTP":false,
        "Hardware":false,
        "Harmony":true,
        "Hermes":false
    }
}

我想过滤这个json以包含其值为true的项目,如下所示

{"status":"On Hold,Completed","application":"EWS,Harmony"}

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

var data = {"status":{"On Hold":true,"Completed":true,"Work In Progress":false,"Closed":false,"Initiation":false},"application":{"Athena":false,"EWindow":false,"EWS":true,"FACT":false,"FTP":false,"Hardware":false,"Harmony":true,"Hermes":false}}

var newData = {};
for(var prop in data){
     var subData = data[prop];
     var xs = [];
     for(var subProp in subData){
         if(subData[subProp] === true)
              xs.push(subProp);
     }
     newData[prop] = xs.join(',');
}