i have a fiddle here 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. 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);
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
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:
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
I agree with @Pavel Gatnar and @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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.