[英]How to group values of two arrays in a third array?
I have bellow this two arrays:我有下面这两个 arrays:
var t1 = ["18:00", "19:00", "20:00"]
var t2 = ["44:00", "23:00", "21:00"]
and I want to group their values in another array like this我想像这样将它们的值分组到另一个数组中
var t3 = ["18:00 - 44:00", "19:00 - 23:00", "20:00 - 21:00"]
I tried some things with nested for loops and maps but I didn't even get close to what I expected.我用嵌套的 for 循环和映射尝试了一些东西,但我什至没有达到我的预期。
You could do it like this:你可以这样做:
var t1 = ["18:00", "19:00", "20:00"] var t2 = ["44:00", "23:00", "21:00"] var t3 = t1.map((_, i) => `${t1[i]} - ${t2[i]}`); console.log(t3);
var t1 = ["18:00", "19:00", "20:00"] var t2 = ["44:00", "23:00", "21:00"] var t3 = [] for(var i = 0; i < t1.length; i++){ t3.push(`${t1[i]} - ${t2[i]}`); }
var t3 = t1.map((t, index) => `${t} - ${t2[index]}`);
var t1 = ["18:00", "19:00", "20:00"]; var t2 = ["44:00", "23:00", "21:00"]; let t3 = []; for (let i=0; i<t1.length; i++) { t3.push(t1[i]+" - "+t2[i]); } console.log(t3);
You can map through the first array and adding the value of the second array , by using the index value:您可以 map 通过使用索引值通过第一个数组并添加第二个数组的值:
var t1 = ["18:00", "19:00", "20:00"]
var t2 = ["44:00", "23:00", "21:00"]
var t3 = t1.map((element, index) => {
return element + ' - ' + t2[index]
})
The map method offers access to the index map 方法提供对索引的访问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.