我有两个数组:"interfaceTitles“,值为"USB ports","digital input”,"RS232,...“interfaceAmounts,值为"3","20","1",...
我需要一个具有组合值的合并数组。因此,新阵列应具有以下值:"3个USB端口“,"20个数字输入”...
所以它不只是连接,它是融合:D
interfacesAdded = interfaceAmounts && interfaceTitles
不工作
interfacesAdded = interfaceAmounts + interfaceTitles
将其转换为字符串
"interfacesAdded“声明为const interfacesAdded: any = ...
我能做些什么呢,搜索功能帮不了我,对不起,我有点没经验:
问候
发布于 2021-03-03 11:43:09
下面使用map
函数遍历interfaceTitles
列表,并返回一个新列表,其中的元素是由空格连接的两个列表中的相应元素。
const result = interfaceTitles.map((_, i) => {
return interfaceAmounts[i] + " " + interfaceTitles[i];
});
希望这就是你要找的。
发布于 2021-03-03 11:47:13
我认为你可以添加你正在使用的语言来帮助更好地回答,不同语言的逻辑将是相同的,我正在用js来做这件事。允许您想要连接的字符串位于两个数组的相同位置:
const array1 = [1, 4, 9, 16];
const array2 = ['a','b','c','d'];
// pass a function to map
// care in js we can use the + to concat string
// map will iterate over each element of array1
// x will be the current element of array1
// index will be the index of the current element,
// used to get the corresponding element in array2
const map1 = array1.map((x,index) => x+array2[index]);
console.log(map1);
// expected output: > Array ["1a", "4b", "9c", "16d"]
https://stackoverflow.com/questions/66456254
复制