虽然我有一些基本的JavaScript背景,但我偶然发现了我写的这段代码:
var data=[{"_id":"57b3e7ec9b209674f1459f36","fName":"Tom","lName":"Moody","email":"Tom@example.com","age":30},{"_id":"57b3e8079b209674f1459f37","fName":"Pat","lName":"Smith","email":"pat@example.com","age":32},{"_id":"57b3e8209b209674f1459f38","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28},{"_id":"57b3e8219b209674f1459f39","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28}]
var tempArr=[];
var table=[];
var dataArr = Object.keys(data).map(function(k) { return data[k] });
dataArr.forEach(function(user) {
tempArr[0]=user.fName;
tempArr[1]=user.lName;
tempArr[2]=user.email;
tempArr[3]=user.age;
table.push(tempArr);
console.log('table'+JSON.stringify(table));
});
在最后一个循环中,我希望表包含Tom,Pat和Sam的数组。相反,这就是我得到的:
table[["Tom","Moody","Tom@example.com",30]]
table[["Pat","Smith","pat@example.com",32],["Pat","Smith","pat@example.com",32]]
table[["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28]]
table[["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28]]
为什么push()替换表中的上一个条目?任何帮助将受到高度赞赏。
答案 0 :(得分:6)
其他人已经在你的代码中指出了问题。
然而,你也使事情变得更加复杂。你可以这样做:
var data=[{"_id":"57b3e7ec9b209674f1459f36","fName":"Tom","lName":"Moody","email":"Tom@example.com","age":30},{"_id":"57b3e8079b209674f1459f37","fName":"Pat","lName":"Smith","email":"pat@example.com","age":32},{"_id":"57b3e8209b209674f1459f38","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28},{"_id":"57b3e8219b209674f1459f39","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28}];
var table = data.map(function(user) {
return [
user.fName,
user.lName,
user.email,
user.age,
];
});
console.log(table);

或者如果您使用ES6:
var table = data.map(user => [ user.fName, user.lName, user.email, user.age ];
答案 1 :(得分:2)
您不需要手动编写所有样板代码。使用正确的数组迭代器(在您的情况下为map
)。
var table = data.map(function(user) {
return [user.fName, user.lName, user.email, user.age];
});
答案 2 :(得分:1)
显然,map是为了功能性方法而去的方式,但是如果你喜欢命令式样式,一种简单的方法可能是使用for of
循环,如下所示。
var data = [{"_id":"57b3e7ec9b209674f1459f36","fName":"Tom","lName":"Moody","email":"Tom@example.com","age":30},{"_id":"57b3e8079b209674f1459f37","fName":"Pat","lName":"Smith","email":"pat@example.com","age":32},{"_id":"57b3e8209b209674f1459f38","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28},{"_id":"57b3e8219b209674f1459f39","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28}],
table = [];
for (var user of data) table.push([user.fName,user.lName,user.email,user.age]);
console.log(table);

答案 3 :(得分:0)
这里的问题不在于推动。 javascript中的变量存储对数组的引用。在表中,您正在推送对相同数组tempArr的引用。您需要在推送之前创建新数组,或者在推送之前创建数组的深层副本。
示例
var data=[{"_id":"57b3e7ec9b209674f1459f36","fName":"Tom","lName":"Moody","email":"Tom@example.com","age":30},{"_id":"57b3e8079b209674f1459f37","fName":"Pat","lName":"Smith","email":"pat@example.com","age":32},{"_id":"57b3e8209b209674f1459f38","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28},{"_id":"57b3e8219b209674f1459f39","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28}]
var table=[];
var dataArr = Object.keys(data).map(function(k) { return data[k] });
dataArr.forEach(function(user) {
var tempArr=[];
tempArr[0]=user.fName;
tempArr[1]=user.lName;
tempArr[2]=user.email;
tempArr[3]=user.age;
table.push(tempArr);
console.log('table'+JSON.stringify(table));
});
答案 4 :(得分:0)
嗯,与许多其他语言不同,JavaScript已经通过引用传递了所有内容。这意味着当您table.push(tempArr);
时,您实际上并没有推动tempArr
的值,而是将引用推送到{{1 }}。所以,如果你这样做:
var a ='a';
tempArr
您将获得var table = [];
table.push(a);
a = 'b';
console.log(table[0]);
作为输出。你想要做的是定义一个要推送的新变量,比如这个
b
答案 5 :(得分:0)
var data = [{"_id": "57b3e7ec9b209674f1459f36","fName": "Tom","lName": "Moody","email": "Tom@example.com","age": 30}, {"_id": "57b3e8079b209674f1459f37","fName": "Pat","lName": "Smith","email": "pat@example.com","age": 32}, {"_id": "57b3e8209b209674f1459f38","fName": "Sam","lName": "Dawn","email": "sam@example.com","age": 28}, {"_id": "57b3e8219b209674f1459f39","fName": "Sam","lName": "Dawn","email": "sam@example.com","age": 28}],
table = [];
data.forEach(function(user) {
table.push([user.fName, user.lName, user.email, user.age]);
});
console.log(table);