Javascript动态地在Object中分配Object

时间:2017-07-18 19:51:01

标签: javascript json object

这是我的对象示例,我需要循环并在每个内部对象中动态分配{" type":" object"}。

输入:

var favoriteFruit = {     "艾米":{        " desc":" Amy' s",         "水果":{             "名称" :" Banana",             "颜色" :" Red"          }     },         " Ben":{            " desc":" Ben''s",            "水果":{                 "名称" :" Apple",                 "颜色" :" Red"              }         },         "卡罗尔":{             " desc":" Carol的收藏",             "水果":{                 "名称" :" Cherry",                 "颜色" :" Red"              }         },         " Olivia":{             " desc":" Olivia fav",             "水果":{                 "名称" :" Orange",                 "颜色" :" Orange"              }         },         " Pualine":{             " desc":" Pualine fav",             "水果":{                 "名称" :" Pear",                 "颜色" :"格林"              }         }     };

预期产出:

var favoriteFruit = {
    "Amy": {
       "type": "object", 
       "desc": "Amy's fav",
         "fruit": {
            "Name" : "Banana",
            "color" : "Red"
         }
    },
    "Ben": {
        "type": "object", 
        "desc": "Ben's fav",
         "fruit": {
            "Name" : "Apple",
            "color" : "Red"
         }
    },
    "Carol": {
        "type": "object",
        "desc": "Carol's fav", 
         "fruit": {
            "Name" : "Cherry",
            "color" : "Red"
         }
    },
    "Olivia": {
        "type": "object",
         "desc": "Olivia fav", 
         "fruit": {
            "Name" : "Orange",
            "color" : "Orange"
         }
    },
    "Pualine": {
        "type": "object", 
        "desc": "Pualine fav",
         "fruit": {
            "Name" : "Pear",
            "color" : "Green"
         }
    }
};

2 个答案:

答案 0 :(得分:0)

你实际上没有JSON字符串,但我引用它现在就是。

见内联评论:

var favoriteFruit = `{
    "Amy": {
       "desc": "Amy's fav",
        "fruit": {
            "Name" : "Banana",
            "color" : "Red"
         }
    },
    "Ben": {
       "desc": "Ben's fav",
       "fruit": {
            "Name" : "Apple",
            "color" : "Red"
         }
    },
    "Carol": {
        "desc": "Carol's fav",
        "fruit": {
            "Name" : "Cherry",
            "color" : "Red"
         }
    },
    "Olivia": {
        "desc": "Olivia fav",
        "fruit": {
            "Name" : "Orange",
            "color" : "Orange"
         }
    },
    "Pualine": {
        "desc": "Pualine fav",
        "fruit": {
            "Name" : "Pear",
            "color" : "Green"
         }
    }
}`;

// Turn the JSON into an object
var obj = JSON.parse(favoriteFruit);

// Loop through the object's properties
for(var prop in obj){
   // Give each object a new property/value
   obj[prop].type = "Object";
}

console.log(obj);

答案 1 :(得分:0)

通过密钥进行迭代,使用Object.keys()可访问,然后使用Object.assign()从那里开始:



var favoriteFruit = {
  "Amy": {
    "desc": "Amy's fav",
    "fruit": {
      "Name": "Banana",
      "color": "Red"
    }
  },
  "Ben": {
    "desc": "Ben's fav",
    "fruit": {
      "Name": "Apple",
      "color": "Red"
    }
  },
  "Carol": {
    "desc": "Carol's fav",
    "fruit": {
      "Name": "Cherry",
      "color": "Red"
    }
  },
  "Olivia": {
    "desc": "Olivia fav",
    "fruit": {
      "Name": "Orange",
      "color": "Orange"
    }
  },
  "Pualine": {
    "desc": "Pualine fav",
    "fruit": {
      "Name": "Pear",
      "color": "Green"
    }
  }
}

Object.keys(favoriteFruit).forEach(function (key) {
  Object.assign(favoriteFruit[key], { type: 'object' })
})

console.log(favoriteFruit)




如果您真的关心type是字符串化时的第一个键(尽管规范中不保证这是因为键最终是无序的),您可以改为使用它:

Object.keys(favoriteFruit).forEach(function (key) {
  favoriteFruit[key] = Object.assign({ type: 'object' }, favoriteFruit[key])
})