请原谅我的主题。无法理解要放什么。
我有一些动态生成的数据。数据与此类似。
var TypeA ={ "length" : "100" , "width" :"80" , "color" : "#ffffff" }
var TypeB ={ "length" : "150" , "width" :"120" , "color" : "#4286f4" }
var TypeC ={ "length" : "150" , "width" :"120" , "color" : "#4202f4" }
以上几个JSON分别定义了类型A,B,C的属性。 (实际上,有很多种类型。)
现在我有如下实际数据:
var data =[{id : "1" , label : "A1" , type : "TypeA" },
{id : "2" , label : "A2" , type : "TypeA" },
{id : "3" , label : "B1" , type : "TypeB" },
{id : "4" , label : "A3" , type : "TypeA" },
{id : "5" , label : "C1" , type : "TypeC" },
{id : "6" , label : "B2" , type : "TypeB" }
]
现在我在data
中运行循环。在此循环中,我需要根据数据JSON中定义的type
访问Type属性。
我正在做这样的事情,但它似乎无法奏效。
$.each(JSON.parse(data), function(index,jsonObject){
console.log("ColorCode : "+ jsonObject.type.color);
});
任何人都可以帮助我如何在这里访问类型属性。感谢。
答案 0 :(得分:3)
在您给出的示例中,您为每个类型定义定义了一个变量。它使他们更难以访问。相反,你应该有这个:
var types = {
"TypeA": { "length" : "100" , "width" :"80" , "color" : "#ffffff" },
"TypeB": { "length" : "150" , "width" :"120" , "color" : "#4286f4" },
"TypeC": { "length" : "150" , "width" :"120" , "color" : "#4202f4" }
};
或者这个:
var TypeA ={ "length" : "100" , "width" :"80" , "color" : "#ffffff" };
var TypeB ={ "length" : "150" , "width" :"120" , "color" : "#4286f4" };
var TypeC ={ "length" : "150" , "width" :"120" , "color" : "#4202f4" };
var types = {
"TypeA": TypeA,
"TypeB": TypeB,
"TypeC": TypeC
};
然后,这很容易做到:
JSON.parse(data).forEach(function(el) {
console.log("ColorCode : " + types[el.type].color);
});
var types = {
"TypeA": { "length": "100", "width": "80", "color": "#ffffff" },
"TypeB": { "length": "150", "width": "120", "color": "#4286f4" },
"TypeC": { "length": "150", "width": "120", "color": "#4202f4" }
};
var data =[
{id : "1" , label : "A1" , type : "TypeA" },
{id : "2" , label : "A2" , type : "TypeA" },
{id : "3" , label : "B1" , type : "TypeB" },
{id : "4" , label : "A3" , type : "TypeA" },
{id : "5" , label : "C1" , type : "TypeC" },
{id : "6" , label : "B2" , type : "TypeB" }
];
data.forEach(function (el) {
console.log("ColorCode : " + types[el.type].color);
});

答案 1 :(得分:1)
我建议您在对象中定义Type
var obj = {
"TypeA": {.... },
"TypeB": {.... },,
"TypeC": {.... },
};
然后您可以轻松使用Bracket notation来访问基于属性的字符串键。
$.each(data, function(index,jsonObject){
console.log("ColorCode : ", obj[jsonObject.type].color);
});
var obj = {
"TypeA": {
"length": "100",
"width": "80",
"color": "#ffffff"
},
"TypeB": {
"length": "150",
"width": "120",
"color": "#4286f4"
},
"TypeC": {
"length": "150",
"width": "120",
"color": "#4202f4"
}
};
var data = [{
id: "1",
label: "A1",
type: "TypeA"
},
{
id: "2",
label: "A2",
type: "TypeA"
},
{
id: "3",
label: "B1",
type: "TypeB"
},
{
id: "4",
label: "A3",
type: "TypeA"
},
{
id: "5",
label: "C1",
type: "TypeC"
},
{
id: "6",
label: "B2",
type: "TypeB"
}
]
$.each(data, function(index,jsonObject){
console.log("ColorCode : ", obj[jsonObject.type].color);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 2 :(得分:-2)
使用window
调用变量。
var TypeA ={ "length" : "100" , "width" :"80" , "color" : "#ffffff" }
var TypeB ={ "length" : "150" , "width" :"120" , "color" : "#4286f4" }
var TypeC ={ "length" : "150" , "width" :"120" , "color" : "#4202f4" }
var dataO = [{id : "1" , label : "A1" , type : "TypeA" },
{id : "2" , label : "A2" , type : "TypeA" },
{id : "3" , label : "B1" , type : "TypeB" },
{id : "4" , label : "A3" , type : "TypeA" },
{id : "5" , label : "C1" , type : "TypeC" },
{id : "6" , label : "B2" , type : "TypeB" }];
/*The data is already parsed.*/
dataO.forEach(function(key, index){
console.log(window[key.type].color);
});
/*dataO.forEach(key => console.log(window[key.type].color));*/