-1

I want to perform the following CRUD operations on this json 1)Update content_available as true for code (EN, CN). this is what i have tried. but it doesnt work.

myObj = { "data": [{ "code": "EN", "language": "English", "content_available": true, "isdefault": true }, { "code": "AR", "language": "Arabic", "content_available": true, "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);
SChavan
  • 57
  • 5

1 Answers1

0

In your code, a for loop is used but the key is ignored... and the check for EN or CN should be || (or), not && (and).

To Update content_available as true for code (EN, CN), the code should be:

 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();
shaochuancs
  • 15,342
  • 3
  • 54
  • 62
  • Hey shaochancs! ? Thanks !! It does work. I didn't know how to use the key in this code. Thanks for teaching me this! – SChavan Jun 24 '17 at 10:40
  • @SChavan you're welcome. If your problem is solved and this answer is helpful, maybe it can be accepted? – shaochuancs Jun 24 '17 at 11:50
  • @shaochancs just accepted your answer. I am sorry I didn't know I had to do that, since I am new to stackoverflow. – SChavan Jun 24 '17 at 17:27