[英]create a new array from array of arrays
I have this array of arrays:我有这个 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 + ' = ');
我曾尝试使用 map,例如
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:假设你的内部 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);
You could use a for loop to iterate through the array, and joining each of the 2nd dimensional arrays.您可以使用 for 循环遍历数组,并加入每个二维 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 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.