最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create an array of objects from multiple arrays - Stack Overflow

programmeradmin0浏览0评论

Given the following arrays:

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

What's the best way performance-wise to generate an array of objects with a similar structure to this:

var items = [
    {id:1,name:"john",country:"AU"},
    {id:2,name:"doe",country:"USA"},
    ...
];

Given the following arrays:

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

What's the best way performance-wise to generate an array of objects with a similar structure to this:

var items = [
    {id:1,name:"john",country:"AU"},
    {id:2,name:"doe",country:"USA"},
    ...
];
Share Improve this question edited Nov 11, 2016 at 0:56 Andrew Li 58k14 gold badges134 silver badges148 bronze badges asked Nov 11, 2016 at 0:54 Alejandro PerezAlejandro Perez 611 gold badge1 silver badge4 bronze badges 3
  • 1 If there's one thing I can't say enough on SO, it's this: make it work first, then worry about performance if and only if performance becomes an issue. How would you solve this if you weren't concerned about performance? – kevin628 Commented Nov 11, 2016 at 0:57
  • What kind of performance? Run-time? Memory? Lines of code? Writability/maintainability? – user663031 Commented Nov 11, 2016 at 3:53
  • I am wondering where you were getting stuck, since a simple solution is to loop from 0 to 2, then each time through the loop push an object containing the relevant values onto a results array. Once you've figured that out, you can move on to using map etc. – user663031 Commented Nov 11, 2016 at 3:56
Add a comment  | 

2 Answers 2

Reset to default 20

You should be able to simply map through all ids, keeping a reference to your index, and build your object based on that index.

var items = ids.map((id, index) => {
  return {
    id: id,
    name: names[index],
    country: countries[index]
  }
});

This is what I get when run the code:

[
    { country=AU, name=john, id=1.0 }, 
    { name=doe, country=USA, id=2.0 }, 
    { id=3.0, country=USA, name=foo }
]

Following is the code, same as @Zack Tanner

function arrs2Obj() {
    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

    var items = ids.map((id, index) => {
        return {
            id: id,
            name: names[index],
            country: countries[index]
        }
    });
    Logger.log(items)
}

The problem is, this result is not sorted as the questioner asked. I mean it's not consistent - ids to be the first item, name second, country 3rd; this way it is more presentable.

发布评论

评论列表(0)

  1. 暂无评论