我是关联数组的新手,下面有这段代码。我想将 和 的值分配prop
给sumprop[prop]
一个新数组,prop1
并且prop2
. 在这里感谢一些帮助。
var name = new Array();
var amount = new Array();
name[0]="Peter";
amount[0]="50";
name[1]="John";
amount[1]="10";
name[2]="John";
amount[2]="20";
name[3]="Peter";
amount[3]="20";
name[4]="Mary";
amount[4]="40";
var sum = {};
var i;
var prop1 = new Array();
var prop2 = new Array();
for (i = 0; i < amount.length; i++) {
var name = name[i];
var oldSum = sum[name];
if (typeof oldSum === 'undefined') {
oldSum = 0;
}
sum[name] = oldSum + amount[i];
}
for (var prop in sum) {
if (sum.hasOwnProperty(prop)) {
console.log(prop + " " sum[prop]);
// assign to a new array
}
}