Simha Chalam Simha Chalam - 3 months ago 11
Node.js Question

how to push objects in to an array?

I created rest full web service in nodeJs by using Sequelize 2.0.2 and mysql
I done this by using eager-loading concept in Sequelize and Sequelize literal
so i want the json data in particular format
Just i gave a sample bit of code
my sample code :

var results=JSON.parse(JSON.stringify(result));
var data=[];
var states;
var districts;
var cities;
var locations;
var sub_locations;
var properties;
var builders;
console.log(JSON.stringify(result));

for (var country in results) {
states=results[country].hp_states;
for(var state in states){
districts=states[state].hp_districts;
data.push({state_id: states[state].state_id, state_name: states[state].state_name});
for(var district in districts) {
cities = districts[district].hp_cities;
data[state]["district_id"]=districts[district].district_id;
data[state]["district_name"]=districts[district].district_name;
for(var city in cities) {
locations = cities[city].hp_locations;
data[district]["city_id"]=cities[city].city_id;
data[district]["city_name"]=cities[city].city_name;

for(var location in locations) {
sub_locations=locations[location].hp_sub_locations;
data[city]["location_id"]=locations[location].location_id;
data[city]["location_name"]=locations[location].location_name;
for(var sublocation in sub_locations){
properties=sub_locations[sublocation].hp_property;

/*data[location]["sub_location_id"]=sub_locations[sublocation].sub_location_id;
data[location]["sub_location_name"]=sub_locations[sublocation].sub_location_name;
console.log(JSON.stringify(properties)+"properties...");
*/
}
}
}

}


}


}


I am getting this result from database:

enter image description here




I want this format:

enter image description here

Json format i am getting:

[{"country_id":1,"country_name":"India","hp_states":[{"state_id":1,"state_name":"Tamil Nadu","hp_districts":[{"district_id":3,"district_name":"Erode","hp_cities":[{"city_id":3,"city_name":"Erode","hp_locations":[{"location_id":141,"location_name":"Chellamma Road","hp_sub_locations":[{"sub_location_id":7,"sub_location_name":"Ganga Road","hp_property":{"property_id":3,"property_name":"golden","hp_builder":{"builders_id":21,"builders_name":"golden homes"}}}]}]}]}]},{"state_id":2,"state_name":"Karnataka","hp_districts":[{"district_id":5,"district_name":"Bangalore","hp_cities":[{"city_id":5,"city_name":"Bangalore","hp_locations":[{"location_id":15,"location_name":"Hebbal","hp_sub_locations":[{"sub_location_id":5,"sub_location_name":"Hebbal Police Station","hp_property":{"property_id":1,"property_name":"Godrej Woodsman Estate","hp_builder":{"builders_id":1,"builders_name":"Godrej"}}},{"sub_location_id":6,"sub_location_name":"Hebbal Ring road","hp_property":{"property_id":2,"property_name":"Skyline","hp_builder":{"builders_id":3,"builders_name":"Skyline"}}}]},{"location_id":14,"location_name":"Marathahalli","hp_sub_locations":[{"sub_location_id":4,"sub_location_name":"Kundanalli Gate","hp_property":{"property_id":4,"property_name":"golden","hp_builder":{"builders_id":21,"builders_name":"golden homes"}}}]}]}]}]}]}]

Answer

Here you go, I changed the location to 'loc', because I tested it in browser, and location is predefined in browser, if you are using node, it is probably OK to use location.

var results=JSON.parse(JSON.stringify(result));
var data=[];
var states;
var districts;
var cities;
var locations;
var sub_locations;
var properties;
var builders;


for (var country in results) {
    states=results[country].hp_states;
    for(var state in states){
        districts=states[state].hp_districts;

        for(var district in districts) {
            cities = districts[district].hp_cities;

            for(var city in cities) {
                locations = cities[city].hp_locations;

                for(var loc in locations) {
                    //location is a browser predefined variable, you can't call your var as location.
                    sub_locations=locations[loc].hp_sub_locations;

                    for(var sublocation in sub_locations){
                        properties=sub_locations[sublocation].hp_property;


                        data.push({
                           state_name: states[state].state_name,
                           district_name:  districts[district].district_name,
                           location_name: locations[loc].location_name,
                           sub_location_id: sub_locations[sublocation].sub_location_id,
                           property_id: properties.property_id,
                           property_name: properties.property_name,
                           builders_id: properties.hp_builder.builders_id,
                           builders_name: properties.hp_builder.builders_name
                        });
                    }
                }
            }
        }
    }
}

console.log(data);//data contains 4 items.
Comments