I have this array of arrays:
let a = [
["i was sent", "i do"],
["i was sent", "i sent"],
["to protect you", "to find you"]
]
And I want to return this single array from it:
b = ["i was sent = i do", "i was sent = i sent", "to protect you = to find you"]
How can I do that?
I have tried to use a map like let b = a.map(s => s + ' = '); but it won't do the job?
asked May 29, 2020 at 17:36
foxer
9112 gold badges7 silver badges21 bronze badges
4 Answers 4
let a = [
["i was sent", "i do"],
["i was sent", "i sent"],
["to protect you", "to find you"]
]
let result = a.map(x => x.join(" = "))
console.log(result)
Unmitigated
91.5k12 gold badges103 silver badges109 bronze badges
answered May 29, 2020 at 17:39
Paul
2,0761 gold badge10 silver badges16 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Assuming your inner arrays always have two elements:
let a = [
["i was sent", "i do"],
["i was sent", "i sent"],
["to protect you", "to find you"]
]
let b = a.map(el => `${el[0]} = ${el[1]}`);
console.log(b);
answered May 29, 2020 at 17:39
Balastrong
4,4602 gold badges14 silver badges32 bronze badges
Comments
You could use a for loop to iterate through the array, and joining each of the 2nd dimensional arrays. You could use something like this:
for(var i = 0 ; i < array.length ; i++) {
array[i] = array[i][0] + " = " + array[i][1];
}
Comments
mine...
let a =
[ [ "i was sent", "i do"]
, [ "i was sent", "i sent"]
, [ "to protect you", "to find you"]
]
const jojo=([x,y])=>x+' = '+y
let b = a.map(jojo)
console.log ( b )
answered May 29, 2020 at 18:08
Mister Jojo
23k6 gold badges28 silver badges45 bronze badges
Comments
lang-js
mapstatement, debug and check whatsis. You'll find it's an array of two strings.