milesma milesma - 4 months ago 9
Javascript Question

How to combine two same lengths array into one same length array with element joined by a separator

Given

a=["a", "b", "c"];
x=["x", "y", "z"];


combine/join the two list: the expected result:

ax=["a:x", "b:y", "c:z"];


How? One liner is better.

Something like python's zip function.

Thanks

Answer

In ES6:

let a = ["a", "b", "c"], 
    x = ["x", "y", "z"]; 
let ax = a.map( (value,index) => value + ':' + x[index] ); 

console.log(ax);
Comments