如何将对象推入数组?

Posted

技术标签:

【中文标题】如何将对象推入数组?【英文标题】:how to push objects in to an array? 【发布时间】:2016-12-25 09:59:57 【问题描述】:

我使用 Sequelize 2.0.2 和 mysql 在 nodeJs 中创建了完整的 web 服务 我通过在 Sequelize 和 Sequelize literal 中使用急切加载概念来做到这一点 所以我想要特定格式的json数据 只是我给了一个示例代码 我的示例代码:

 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...");
*/
                            
                        
                    

                


            


        

我从数据库中得到这个结果:


我想要这种格式:

我得到的 JSON 格式:

["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"]]]]]]

【问题讨论】:

你的方法有什么问题? 感谢回复,假设如果有一个位置是(hebbal)--->包含两个子位置(id,name)(1,hebbal ring road),(2,hebbal警察局)我想在 (banglore) 中创建类似的单独对象。但它的覆盖只给出一个对象,如 data=[cityname:'banglore',location:'hebbal',sub_location:'hebbal policestation'] 。所以我要创建两个单独的对象,如 data=[cityname :'banglore',location:'hebbal',sub_location:'hebbal 警察局',[cityname:'banglore',location:'hebbal',sub_location:'hebbal ring road'] JSON 的结构是什么?你8能提供一个数据样本吗?因为我在 JSON 的根目录中看到它可能是也可能不是“国家”,它可能直接是“州” 请查看 json 美化器中的数据:查看我得到的顶部 Json 格式 【参考方案1】:

你去,我把位置改成'loc',因为我在浏览器中测试过,位置是在浏览器中预定义的,如果你使用节点,使用位置应该是可以的。

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.

【讨论】:

以上是关于如何将对象推入数组?的主要内容,如果未能解决你的问题,请参考以下文章

如何将对象推入数组?

如何使用 R 将 json 对象推入数组

如何使用扩展运算符将对象数组推入特定元素

如何将父对象迭代到子对象并推入数组javascript(vuejs)

推入 mongoose 对象内部的数组

将对象推入数组