I am looking to include the following functionality in the JSON data provided below: "Check if the key name default exists, and if it does, add another key within the same object => ("pin" : 91)." I have attempted to achieve this using the code snippet below.
var 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 addKey(k) {
for (var key in myObj.data) {
if (myObj["data"][key] === k) {
myObj["data"][key]["pin"] = "91";
}
}
}
addKey("default");
console.log(myObj);