繁体   English   中英

根据另一个JSON对象的值访问JSON对象

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM