我正在学习Javascript中的不同数组函数,并且无法理解我编写的用于测试array.map()的基本代码的输出。
let contacts = [{
"firstName": "Jim",
"lastName": "Smith"
}, {
"firstName": "Laura",
"lastName": "Bush"
}, {
"firstName": "Adam",
"lastName": "Shaw"
}];
let tempJson = {};
const newContacts = contacts.map(contact => {
//tempJson = {}
tempJson[contact.firstName] = contact.lastName
console.log(tempJson);
return tempJson;
});
console.log(newContacts);
预期产量
//tempJson
{ "Jim": "Smith" }
{ "Jim": "Smith", "Laura": "Bush" }
{ "Jim": "Smith", "Laura": "Bush", "Adam": "Shaw" }
//newContacts
[ { "Jim": "Smith", },
{ "Jim": "Smith", "Laura": "Bush"},
{ "Jim": "Smith", "Laura": "Bush", "Adam": "Shaw" } ]
实际产量
//tempJson
{ "Jim": "Smith" }
{ "Jim": "Smith", "Laura": "Bush" }
{ "Jim": "Smith", "Laura": "Bush", "Adam": "Shaw" }
//newContacts
[ { "Jim": "Smith", "Laura": "Bush", "Adam": "Shaw" },
{ "Jim": "Smith", "Laura": "Bush", "Adam": "Shaw" },
{ "Jim": "Smith", "Laura": "Bush", "Adam": "Shaw" } ]
新的通讯录数组不应该只包含map函数返回的对象吗?
我错过了一些东西,我不确定它是什么。
互换的青春
月关宝盒
幕布斯6054654
相关分类