0

I want to join js array for csv like. So, how to simply(no use "for" if I can) write code please tell me. Only use Chrome.

let arr1 = ["hoge","piyo", "fuga"];
let arr2 = ["foo", "bar", "baz"];
//Want to array
let arrResult = ["hoge,foo","piyo,bar", "fuga,baz"];
Makoto Uno
  • 33
  • 5

3 Answers3

3

Here's a simple Array.map() to get there.

let arr1 = ["hoge", "piyo", "fuga"];
let arr2 = ["foo", "bar", "baz"];
let combo = arr1.map((el, i) => [el, arr2[i]].join(","))
console.log(combo)
Kinglish
  • 23,358
  • 3
  • 22
  • 43
0

is this what you're looking for?

let arr1 = ["hoge", "piyo", "fuga"];
let arr2 = ["foo", "bar", "baz"];
let arrResult = [];

for(let i=0; i<arr1.length; i++){
  arrResult[i] = `${arr1[i]},${arr2[i]}`;
}

console.log(arrResult);
ahsan
  • 1,409
  • 8
  • 11
0

Try this.

let arr1 = ["hoge", "piyo", "fuga"];
let arr2 = ["foo", "bar", "baz"];
let arrResult = [];
arr1.forEach((item,index) => {
  if(index < arr2.length){
     arrResult[index] = `${item},${arr2[index]}`;
  }
})
M.Hassan Nasir
  • 851
  • 2
  • 13