简体   繁体   中英

how to get key value from json where value is true

i have a json, i need to get the key value from json where the value which are true.

here is what i expect.

Expected:

{
      "isChrome": true,
      "isDesktop": true,
      "isWindows": true  
}

Problem:

    {
      "isMobile": false,
      "isiPad": false,
      "isiPod": false,
      "isiPhone": false,
      "isAndroid": false,
      "isBlackberry": false,
      "isOpera": false,
      "isIE": false,
      "isIECompatibilityMode": false,
      "isSafari": false,
      "isFirefox": false,
      "isWebkit": false,
      "isChrome": true,
      "isKonqueror": false,
      "isOmniWeb": false,
      "isSeaMonkey": false,
      "isFlock": false,
      "isAmaya": false,
      "isEpiphany": false,
      "isDesktop": true,
      "isWindows": true,
      "isLinux": false,
      "isMac": false,
      "isBada": false,
      "isSamsung": false,
      "isRaspberry": false,
      "isBot": false,
      "isCurl": false,
      "isAndroidTablet": false,
      "Browser": "Chrome",
      "Version": "32.0.1700.76",
      "OS": "Windows 7",
      "Platform": "Microsoft Windows",
      "GeoIP": {},
      "source": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
    }
var result = {};
for (var key in data) {
    if (data[key] === true) {
        result[key] = true;
    }
}
console.log(result);

Output

{ isChrome: true, isDesktop: true, isWindows: true }
var result = {};
for (var key in input.Problem) {
    if (input.Problem[key] === true) {
        result[key] = true;
    }
}

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