简体   繁体   中英

Filtering a JSON

I am having a JSON like below

{
    "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
    }
}

I want to filter this json to contain item whose value is true alone like below

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

Any help is much appreciated

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(',');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM