使用JAVASCRIPT将名称值对添加到JSON中的数组对象

时间:2017-06-24 18:03:18

标签: javascript json crud

我想实现这个: "检查密钥名称默认是否可用,如果可用,则在同一对象中再添加一个密钥。 => (" pin":91)"在下面给出的json上。我试过以下代码。

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);

2 个答案:

答案 0 :(得分:0)

function checkForKey(obj, key) {
    for (var i = 0; i < obj.data.length; i++) {
        if (obj.hasOwnProperty(obj.data[i]) && obj.data[i][key] !== undefined) {
            obj.data[i].pin = 91;
        }
    }
}

checkForKey(myObj, 'default');

首先,我们获取数据密钥。因为它是一个数组,我们迭代它。然后,我们检查默认密钥是否具有已定义的值,并仅在使用任何值定义密钥引脚时添加密钥引脚。

答案 1 :(得分:0)

您需要做的就是遍历键名称,如果不存在,则创建它。但是,记住你的类型。 for/in用于迭代对象,而不是数组,forEach()是数组的首选。

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) {

  // myObj.data is an array. for/in loops should not be used on arrays
  // But forEach() is preferred on arrays
  myObj.data.forEach(function(obj){
    
    // Each array element is an object. We need to loop that object.
    // for/in loops are for objects
    for(var prop in obj){
      // Is the current property (key) name the same as the provided argument ("default")
      if(prop === k){
         // Add new key/value to the object
         obj["pin"] = "91";
      } 
    }
  });
}
addKey("default");
console.log(myObj);