使用核心javascript的json crud操作

时间:2017-06-24 08:11:43

标签: javascript json crud using operations

我想在这个json上执行以下CRUD操作         1)将代码(EN,CN)的content_available更新为true。         这是我试过的。但它不起作用。

myObj = {         “数据”:[{             “code”:“EN”,             “英语语言”,             “content_available”:是的,             “isdefault”:是的         },{             “code”:“AR”,             “语言”:“阿拉伯语”,             “content_available”:是的,             “isdefault”:false,                     “default”:true

    }, {
        "code": "BR",
        "language": "Brazilian Portuguese",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "CN",
        "language": "Simplified Chinese",
        "content_available": true,
        "isdefault": false,
                "default" : true
    }, {
        "code": "TW",
        "language": "Traditional Chinese",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "DE",
        "language": "German",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "ES",
        "language": "Spanish",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "FR",
        "language": "French",
        "content_available": true,
        "isdefault": false
    }, {
        "code": "JP",
        "language": "Japanese",
        "content_available": true,
        "isdefault": false,
                 "default" : true
    }, {
        "code": "RU",
        "language": "Russian",
        "content_available": false,
        "isdefault": false
    }],
    "success": true
    }

 function setContentAvailable() {
     for (var key in myObj.data) {
         if (myObj["data"]["code"] === "EN" && myObj[data][code] === "CN") {
             myObj.data.content_available = false;
         }
     }
 }
 setContentAvailable();
 console.log(myObj);

1 个答案:

答案 0 :(得分:0)

在您的代码中,使用了for循环,但忽略了key ... ENCN的检查应为|| (或),而不是&&(和)。

将content_available更新为true代码(EN,CN),代码应为:

 function setContentAvailable() {
   for (var key in myObj.data) {
     if (myObj["data"][key]["code"] === "EN" || myObj["data"][key]["code"] === "CN") {
         myObj["data"][key]["content_available"] = true;
     }
   }
 }
 setContentAvailable();