Combine two arrays into one array javascript

javascript

Solution

Very simple, first we create a result array, then we iterate through the first array and add elements to it. Here is a working fiddle.

Note, in your code you have notation like `{5,10}` which is illegal in JavaScript, I assumed you mean an array.

var result = [];
for(var i=0;i<array1.length;i++){
   result.push([array1[i],array2[i]]);
}

Update after edit , it seems like you want objects, try

var result = [];
for(var i=0;i<array1.length;i++){
   result.push({a:array1[i],b:array2[i]});//add object literal
}

If you'd like, you can also use `map` and write the same code functionally. Here is a fiddle of that sort of implementation

Problem

I have two arrays currently ``` array1 = [5, 10, 20] array2 = [10, 20, 30] ``` Either array3 or something like this: ``` array4 = [{"a":5, "b":10}, {"a":10, "b":20}, {"a":20, "b":30}] ``` I know this is probably an easy question but I'm not even sure what array3 would be called so its kind of hard to google this.

Original source

Related problems