在数组中添加键值对象对

时间:2015-06-14 09:44:34

标签: javascript

function Person (name, age) {
  this.name = name;
  this.age = age;
}

var family = []

var people = {alice:40, bob:42, michelle:8, timmy:6};


for (var key in people) {
  family.push({key:people[key]})
}

console.log(family);

这不给我钥匙。它给予的关键'为每个键。 在数组中添加{key:value}对象的正确方法是什么?

更新 基于下面的K3N解决方案,如果我们每次都从构造函数声明,这是我理解的最佳方法:

keys = Object.keys(people);
for (var i = 0; i < keys.length; i++) {
  family.push(new Person(keys[i], people[keys[i]]));
}

2 个答案:

答案 0 :(得分:3)

因为你的人物数组包含人的名字作为关键,而值是年龄(小心,好像你得到两个同名的人在时空中发生核反应......!)。

你可以这样做:

&#13;
&#13;
Jun 14, 2015 11:44:01 AM org.apache.http.impl.client.DefaultHttpClient tryConnect
INFO: I/O exception (java.net.SocketException) caught when connecting to {}->http://www.destination.com: SOCKS : authentication failed
Jun 14, 2015 11:44:01 AM org.apache.http.impl.client.DefaultHttpClient tryConnect
INFO: Retrying connect to {}->http://www.www.destination.com
Jun 14, 2015 11:44:03 AM org.apache.http.impl.client.DefaultHttpClient tryConnect
INFO: I/O exception (java.net.SocketException) caught when connecting to {}->http://www.www.destination.com: SOCKS : authentication failed
Jun 14, 2015 11:44:03 AM org.apache.http.impl.client.DefaultHttpClient tryConnect
INFO: Retrying connect to {}->http://www.www.destination.com
Jun 14, 2015 11:44:05 AM org.apache.http.impl.client.DefaultHttpClient tryConnect
INFO: I/O exception (java.net.SocketException) caught when connecting to {}->http://www.www.destination.com: SOCKS : authentication failed
Jun 14, 2015 11:44:05 AM org.apache.http.impl.client.DefaultHttpClient tryConnect
INFO: Retrying connect to {}->http://www.www.destination.com
Jun 14, 2015 11:44:07 AM totalcontrol.TotalControl main
SEVERE: null
java.net.SocketException: SOCKS : authentication failed
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:474)
    at java.net.Socket.connect(Socket.java:589)
    at     org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
...
&#13;
&#13;
&#13;

答案 1 :(得分:2)

使用Bracket notation以便可以将密钥视为变量属性名称

for (var key in people) {
    var temp={};
    temp[key]=people[key]
    family.push(temp)
}

Computed property name

for (var key in people) {
  family.push({[key]:people[key]})
}