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
  • 811
  • 1
  • 6
  • 16

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
  • 76,500
  • 11
  • 62
  • 80
Paul
  • 2,086
  • 1
  • 8
  • 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,336
  • 2
  • 12
  • 31
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
  • 911
  • 9
  • 27
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
  • 20,093
  • 6
  • 21
  • 40