gpt4 book ai didi

javascript - 在javascript或jquery中合并数组

转载 作者:搜寻专家 更新时间:2023-11-01 04:50:11 24 4
gpt4 key购买 nike

var first = [ "a", "b", "c" ];
var second = [ "d", "e", "f" ];

我的输出应该是

var final=["a~d","b~e","c~f"];

其中 '~' 是分隔符。

最佳答案

检查两个数组的长度

查看代码内嵌注释:

var first = [ "a", "b", "c" ];
var second = [ "d", "e", "f" ];

var len = first.length > second.length ? first.length : second.length;
// Get the length of the array having max elements

var separator = '~';

var final = [];

for(var i = 0; i < len; i++) {
// If no element is present, use empty string
first[i] = first[i] ? first[i] : '';
second[i] = second[i] ? second[i] : '';

final.push(first[i] + separator + second[i]);
// Add the new element in the new array
}

关于javascript - 在javascript或jquery中合并数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com