简体   繁体   English

如何在第三个数组中对两个 arrays 的值进行分组?

[英]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.

相关问题 比较两个数组,并用第三个数组中的值替换重复项 - Compare two Arrays and replace Duplicates with values from a third array JS按具有两个不同数组和总和值的组对数组值进行分组 - JS group array values by groups with two different arrays and sum values 比较两个具有不同数量属性的数组,并使用javascript根据条件将第三个数组与比较值映射 - compare two arrays having different number of properties and mapping a third array with compared values on condition using javascript 将两个数组的值(平均值)分组 - Group (average) values from two arrays 在第三个数组中检测两个 arrays 和 output 字符串中的不同单词 - Detect different words in strings of two arrays and output them in third array 如何将数组中的排序数组合并到第三排序数组中 - How to merge sorted arrays within arrays into a third sorted array 两个数组,根据第一个的重复值将第二个数组分组,并形成一个字符串 - two arrays, group second array based on repetitive values of first and form a string 如何根据两个现有 arrays 的值创建新的对象数组? - How to create a new array of objects based on the values of two existing arrays? 如何计算具有循环的数组的子数组中存在的两个值的差值? - How to calculate the difference of two values that are present in sub arrays of an array with a loop? 比较两个数组时如何更改数组中的值 - How to change values in an array when comparing two arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM