[英]Access JSON object based on the value of another JSON object
請原諒我的主題。 不知道要放什么。
我有一些動態生成的數據。 數據與此類似。
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
。在這個循環中我需要根據訪問類型的屬性type
數據JSON定義。
我正在做類似的事情,但是似乎沒有用。
$.each(JSON.parse(data), function(index,jsonObject){
console.log("ColorCode : "+ jsonObject.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 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); });
我建議您在對象中定義Type
var obj = {
"TypeA": {.... },
"TypeB": {.... },,
"TypeC": {.... },
};
那么您可以輕松地使用括號表示法來訪問基於屬性的字符串鍵。
$.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>
用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));*/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.