如何在JavaScript中使用For-Each循环来创建一个对象,其键和值是动态提供的?

时间:2015-10-07 11:25:03

标签: javascript json angularjs

如何使用以下形式制作JavaScript对象,其中Car Make和Model Names由其他变量提供。

    <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">

 <persistence-unit name="up2u">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/up2u_user?autoReconnect=true&amp;useUnicode=yes&amp;characterEncoding=UTF-8" />
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="up2u_user2"/>
            <property name="hibernate.connection.password" value="e2f2c2ac87"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.temp.use_jdbc_metadata_defaults" value="true"/>


            <!-- Connectionpool-->
            <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
            <property name="hibernate.c3p0.max_size" value="200" />
            <property name="hibernate.c3p0.min_size" value="10" />
            <property name="hibernate.c3p0.max_statements" value="400" />
            <property name="hibernate.c3p0.acquireIncrement" value="10"/>
            <property name="hibernate.c3p0.acquireRetryAttempts" value="3"/>
            <property name="hibernate.c3p0.acquireRetryDelay" value="100"/>
            <property name="hibernate.c3p0.checkoutTimeout" value="300" />
            <property name="hibernate.c3p0.timeout" value="300" />
            <property name="hibernate.c3p0.idle_test_period" value="300" />
            <property name="hibernate.c3p0.idleConnectionTestPeriod" value="300" />
            <property name="hibernate.c3p0.maxConnectionAge" value="300" />
            <property name="hibernate.c3p0.maxStatementsPerConnection" value="2" />
            <property name="hibernate.c3p0.testConnectionOnCheckin" value="true"/>

        </properties>
    </persistence-unit>
</persistence>

应使用以下变量形成对象:

var type =“sedan”(轿车类型的对象)
var makes = [“Jaguar”,“AUDI”]; (所有制造的轿车都在这个阵列中)
var modelList = [{“make”:“Jaguar”,“models”:[“XF”,“XJ”]},{“make”:“AUDI”,“models”:“A6”,“A4”}] (特定品牌的所有型号)

2 个答案:

答案 0 :(得分:1)

尝试以下代码。我使用forEach方法进行迭代。 forEach()方法每个数组元素执行一次提供的函数。您可以使用任何迭代技术,例如forwhile等。

var type = "sedan";
var makes = ["Jaguar", "AUDI"];
var modelList = [{
    "make": "Jaguar",
    "models": ["XF", "XJ"]
}, {
    "make": "AUDI",
    "models": ["A6", "A4"]
}];

var object = {};
object[type] = {}; //object becomes { "sedan": {} }
makes.forEach(function(make) {
    object[type][make] = []; //In 1st iteration object becomes { "sedan": { "Jaguar": [] }}
    modelList.forEach(function(model) {
        if (model.make == make) {
            object[type][make] = model.models; //In 1st iteration object becomes { "sedan": { "Jaguar": ["XF","XJ"] }} 
        }
    });
});

alert(JSON.stringify(object));

答案 1 :(得分:-1)

给定对象:

var object = {  
"Sedan":{  
  "Jaguar":[  
     "XF",
     "XJ"
  ],
  "AUDI":[  
     "A6",
     "A4"
  ]
}
}

您可以使用以下命令将其设为JSON字符串:

var json = JSON.stringify( object );