简体   繁体   中英

create a new array from array of arrays

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?

 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)

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

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

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM