简体   繁体   English

创建多维数组和JSON解析

[英]creating Multi-dimentional array and JSON parsing

i have a fiddle here http://jsfiddle.net/prantikv/eqqd6xfm/3/ 我在这里有一个小提琴http://jsfiddle.net/prantikv/eqqd6xfm/3/

i have data as such 我有这样的数据

var info={
"company1":[
   {"employee":"*2"},
   {"rooms":"*6"},
   {"vehicals":"3"},
],

"company2":[
    {"employee":"*2"},
    {"rooms":"*6"},
    {"vehicals":"3"},
],
"company3":[
    {"employee":"*2"},
    {"rooms":"*6"},
    {"vehicals":"3"},
]

i get the data from an a json file. 我从一个json文件中获取数据。 what i want to do is that i want to create individial company variables so that i can load them up quickly without other company data 我想做的是创建个人公司变量,这样我就可以快速加载它们而无需其他公司数据

so what i do is this 所以我要做的是

var companiesArray=[];
for(company in info){
console.log("company--> "+company);//company1,2,etc 
var detailsArray=[];

for(var i=0;i<info[company].length;i++)   
{
 //loop through the inner array which has the detials
    for(var details in info[company][i]){
     var detailsValue=info[company][i][details];
    detailsArray[details]=detailsValue;
   }
 }

 companiesArray[company]=[company,detailsArray];
}    

console.log(companiesArray); console.log(companiesArray);

so when i try to get the data i have to do something like this 所以当我尝试获取数据时,我必须做这样的事情

companiesArray['company1'][1].employee

what i want to do is this 我想做的是这个

companiesArray['company1'].employee

where am i going wrong? 我要去哪里错了?

If You don't want to /cannot change the JSON, simply change 如果您不想/无法更改JSON,只需更改

detailsArray = []

to

detailsObject = {}

and

companiesArray[company]=[company,detailsArray];

to

companiesArray[company]=detailsObject;

Now you can use 现在您可以使用

companiesArray['company1'].employee

You have to form your json like this: 您必须像这样形成json:

var info={
  "company1": {
    "employee":"*2",
    "rooms":"*6",
    "vehicals":"3"
  },    
  "company2": {
    "employee":"*2",
    "rooms":"*6",
    "vehicals":"3"
  },
  "company3": {
    "employee":"*2",
    "rooms":"*6",
    "vehicals":"3"
  }
};

Use curly braces ({}) instead of brackets ([]) in order to create objects accessible with the dot notation like this: info.company1.employee 使用花括号({})代替方括号([]),以创建点符号可访问的对象,如下所示: info.company1.employee

I agree with @Pavel Gatnar and @Guillaume. 我同意@Pavel Gatnar和@Guillaume。 This is a very bad data structure. 这是一个非常糟糕的数据结构。 If you can't change the structure only then should you use the following code. 如果您不能仅更改结构,则应使用以下代码。

    var info = {
        "company1": [{
            "employee": "*2"
        }, {
            "rooms": "*6"
        }, {
            "vehicals": "3"
        }],
        "company2": [{
            "employee": "*2"
        }, {
            "rooms": "*6"
        }, {
            "vehicals": "3"
        }],
        "company3": [{
            "employee": "*2"
        }, {
            "rooms": "*6"
        }, {
            "vehicals": "3"
        }]
    };

    var companies = {},
        companyNames = Object.keys(info);

    companyNames.forEach(function (companyName) {
       var companyInfo =  info[companyName],
           company = {};

       companyInfo.forEach(function (properties) {
            var innerPropertyName = Object.keys(properties);

            innerPropertyName.forEach(function (property) {
                company[property] = properties[property];
            });
       });
       companies[companyName] = company;
    });

    console.log(companies);

check out the fiddle here 这里查看小提琴

Try this: 尝试这个:

var info={
    "company1":[
       {"employee":"*2"},
       {"rooms":"*6"},
       {"vehicals":"3"},
    ],

    "company2":[
        {"employee":"*2"},
        {"rooms":"*6"},
        {"vehicals":"3"},
    ],
    "company3":[
        {"employee":"*2"},
        {"rooms":"*6"},
        {"vehicals":"3"},
    ]
};
var companiesArray = {};
for(company in info){
    var detailsArray = {};
    for(var i=0;i<info[company].length;i++) {
        for(var details in info[company][i]){ 
            var detailsValue=info[company][i][details];
            detailsArray[details]=detailsValue;
        }
     }
     companiesArray[company]=detailsArray;
}
console.log(companiesArray['company1'].employee);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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