我找到了this question,但是它已关闭,作者将其范围缩小到了jQuery,并且答案仅适用于两个数组大小相等的情况。
所以我的问题是如何合并元素交替的两个任意数组? (作为回答,提供函数namespace Delivery
{
public partial class Form2: Form
{
public static string passingtext;
public static string passingtext1;
public static string passingtext2;
public static string passingtext3;
public Form2()
{
InitializeComponent();
}
private void datagrid_Load(object sender, EventArgs e)
{
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
row.Cells[0].Value = Todatagrid.passingtext;
row.Cells[1].Value = Todatagrid.passingtext1;
row.Cells[2].Value = Todatagrid.passingtext2;
row.Cells[3].Value = Todatagrid.passingtext3;
dataGridView1.Rows.Add(row);
}
}
}
,它接受两个数组m(a,b)
和a
并返回合并的数组)
测试用例:
b
答案 0 :(得分:2)
一个非常简单的方法是循环检查值是否存在。如果是,请按其他继续。
function alternateMerge(a1, a2) {
var length = Math.max(a1.length, a2.length);
var output = [];
for(var i = 0; i< length; i++) {
if (!!a1[i]) {
output.push(a1[i])
}
if (!!a2[i]) {
output.push(a2[i])
}
}
return output;
}
var as = [1,2,3];
var am = [1,2,3,4,5];
var al = [1,2,3,4,5,6,7];
var b = ["a","b","c","d","e"];
console.log(alternateMerge(as, b).join())
console.log(alternateMerge(am, b).join())
console.log(alternateMerge(al, b).join())
function alternateMerge(a1, a2) {
const arr = a1.length > a2.length ? a1 : a2;
return arr.reduce((acc, _, i) => {
!!a1[i] && acc.push(a1[i]);
!!a2[i] && acc.push(a2[i]);
return acc;
}, [])
}
var as = [1,2,3];
var am = [1,2,3,4,5];
var al = [1,2,3,4,5,6,7];
var b = ["a","b","c","d","e"];
console.log(alternateMerge(as, b).join())
console.log(alternateMerge(am, b).join())
console.log(alternateMerge(al, b).join())
答案 1 :(得分:2)
您可以移动所有元素并将其添加到结果中。
const as = [1, 2, 3];
const am = [1, 2, 3, 4, 5];
const al = [1, 2, 3, 4, 5, 6, 7];
const b = ["a", "b", "c", "d", "e"];
function m(a, b) {
const l = Math.max(a.length, b.length);
const result = [];
for (let i = 0; i < l; i++) {
if (a[i] !== undefined) {
result.push(a[i]);
}
if (b[i] !== undefined) {
result.push(b[i]);
}
}
console.log(result);
return result;
}
m(as, b); // -> [1,"a",2,"b",3,"c","d","e"]
m(am, b); // -> [1,"a",2,"b",3,"c",4,"d",5,"e"]
m(al, b); // -> [1,"a",2,"b",3,"c",4,"d",5,"e",6,7]
答案 2 :(得分:1)
您可以将array#concat
与spread syntax
结合使用以生成合并的数组。
var m = (a,b) => {
const minLen = Math.min(a.length, b.length);
return [].concat(...a.slice(0, minLen).map((v,i) => [v, b[i]]), a.slice(minLen, a.length), b.slice(minLen, b.length));
};
var as = [1,2,3];
var am = [1,2,3,4,5];
var al = [1,2,3,4,5,6,7];
var b = ["a","b","c","d","e"];
console.log(m(as,b)); // -> [1,"a",2,"b",3,"c","d","e"]
console.log(m(am,b)); // -> [1,"a",2,"b",3,"c",4,"d",5,"e"]
console.log(m(al,b)); // -> [1,"a",2,"b",3,"c",4,"d",5,"e",6,7]
答案 3 :(得分:0)
您可以这样做:
const as = [1,2,3];
const am = [1,2,3,4,5];
const al = [1,2,3,4,5,6,7];
const b = ["a","b","c","d","e"];
const m = (a, b) => (a.length > b.length ? a : b)
.reduce((acc, cur, i) => a[i] && b[i] ? [...acc, a[i], b[i]] : [...acc, cur], []);
console.log(m(as,b)); // -> [1,"a",2,"b",3,"c","d","e"]
console.log(m(am,b)); // -> [1,"a",2,"b",3,"c",4,"d",5,"e"]
console.log(m(al,b)); // -> [1,"a",2,"b",3,"c",4,"d",5,"e",6,7]
.as-console-wrapper { max-height: 100% !important; top: 0; }