json 使用核心的 crud 操作 javascript

json crud operations using core javascript

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

我的对象 = { "data":[{ "code": "EN", "language": "English", "content_available": 是的, "isdefault":真 }, { "code": "AR", "language": "Arabic", "content_available": 是的, "isdefault":错误, "default" : 真

    }, {
        "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);

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

为了 更新 content_available 为代码(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();