我们如何获得动态创建的特定键值,例如
汽车对象是动态的
var car = {type:"Fiat", model_2017:"500", color:"white"};
var car = {type:"Fiat", model_2016:"500", color:"white"};
var car = {type:"Fiat", model_2015:"500", color:"white"};
如何获取第二个字段的值,这是动态的,我正在尝试这样但是它给出了未定义的
var model_year=2015;
model_year= "model_"+model_year
car.model_year
答案 0 :(得分:2)
您可以尝试使用[]
语法
car[model_year]
var car = {type:"Fiat", model_2017:"500", color:"white"};
var car = {type:"Fiat", model_2016:"500", color:"white"};
var car = {type:"Fiat", model_2015:"500", color:"white"};
var model_year=2015;
model_year= "model_"+model_year
console.log(car[model_year])