Ale*_*rez 2 javascript arrays ecmascript-6
给定以下数组:
var ids = [1,2,3]; //Hundreds of elements here
var names = ["john","doe","foo"]; //Hundreds of elements here
var countries = ["AU","USA,"USA"]; //Hundreds of elements here
Run Code Online (Sandbox Code Playgroud)
生成具有与此类似结构的对象数组的最佳性能方法是什么:
var items = [
{id:1,name:"john",country:"AU"},
{id:2,name:"doe",country:"USA"},
...
];
Run Code Online (Sandbox Code Playgroud)
Zac*_*ner 10
您应该能够简单地映射所有 id,保留对索引的引用,并根据该索引构建对象。
var items = ids.map((id, index) => {
return {
id: id,
name: names[index],
country: countries[index]
}
});
Run Code Online (Sandbox Code Playgroud)