0

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?

foxer
  • 661
  • 4
  • 12

4 Answers4

3

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
  • 46,070
  • 7
  • 44
  • 60
Paul
  • 2,016
  • 1
  • 7
  • 16
1

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);
Balastrong
  • 4,089
  • 2
  • 10
  • 28
1

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]; }

cs1349459
  • 491
  • 8
  • 21
1

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 )
Mister Jojo
  • 16,804
  • 3
  • 16
  • 39