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?

asked May 29, 2020 at 17:36
2
  • 2
    Does this answer your question? Merge/flatten an array of arrays Commented May 29, 2020 at 17:37
  • In your map statement, debug and check what s is. You'll find it's an array of two strings. Commented May 29, 2020 at 17:38

4 Answers 4

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
91.5k12 gold badges103 silver badges109 bronze badges
answered May 29, 2020 at 17:39
Sign up to request clarification or add additional context in comments.

Comments

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);

answered May 29, 2020 at 17:39

Comments

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

answered May 29, 2020 at 18:00

Comments

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 )

answered May 29, 2020 at 18:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.