簡體   English   中英

從 arrays 數組創建一個新數組

[英]create a new array from array of arrays

我有這個 arrays 數組:

let a = [

["i was sent", "i do"],
["i was sent", "i sent"],
["to protect you", "to find you"]

]

我想從中返回這個數組:

b = ["i was sent = i do", "i was sent = i sent", "to protect you = to find you"]

我怎樣才能做到這一點?

我曾嘗試使用 map,例如let b = a.map(s => s + ' = '); 但它不會做這項工作?

 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)

假設你的內部 arrays 總是有兩個元素:

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

您可以使用 for 循環遍歷數組,並加入每個二維 arrays。 你可以使用這樣的東西:
for(var i = 0; i < array.length; i++) { array[i] = array[i][0] + " = " + array[i][1]; }

礦...

 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 )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM