//{"name":0}
let ArraryOne = [ ];
for(let i = 0; i < 10; i++){
ArraryOne.push({"_name":i});
}
let ArraryTwo= [ ]
let tIndex = 0;
//这个是我现有的,感觉不太对。如果用到One数组的前100位放到Two数组中,这种就很不对
//(不考虑具体需求)
for (let tL = 0; tL < tLength; tL++) {
let tArrary: any[] = []
for (let index = tIndex; index < ArraryOne.length; index += 3) {
if (ArraryOne[index] != null) {
tArrary.push(ArraryOne[index]);
if (this.ArraryOne[index + 1] != null) {
tArrary.push(this.ArraryOne[index + 1]);
if (this.ArraryOne[index + 2] != null) {
tArrary.push(this.ArraryOne[index + 2]);
}
}
}
if (Array.isArray(tArrary)) {
ArraryTwo[tL] = tArrary;
tIndex = index += 3;
break;
}
}
}
//前n,插入位置m
var n = 3,m = 2;
var a = ["a1", "a2", "a3", "a4", "a5"];
var b = ["b1", "b2", "b3", "b4", "b5"];
//场景一、将前n作为数组插入集合位置m
//b.splice(m, 0, a.slice(0, n));
//console.log(b) //['b1', 'b2', Array(3), 'b3', 'b4', 'b5']
//场景二、将前n逐个插入位置m,并依次往后继续插入,使之成为一个同类型数组
a.slice(0, n).forEach((item, i) => {
b.splice(m + i, 0, item);
})
console.log(b) // ['b1', 'b2', 'a1', 'a2', 'a3', 'b3', 'b4', 'b5']
//以上,希望可以帮助到你。
感谢提供的思路:
以下是我改的需要的方式
var eCount = 3; //a数组几个为一组
var startCount = 0; //b数组开始位置获取aAr的索引
var aAr = ["a1", "a2", "a3", "a4", "a5","a6","a7"]; //A数组中每3个数据放到B数组中的0,1,2,按顺序插入 不足也可以
var bAr = [];
for (let index = 0; index < Math.ceil(aAr.length / eCount); index++) {
bAr.splice(index,0,aAr.slice(startCount,eCount+startCount));
startCount+=eCount;
}
cc.log(bAr);//Array(3) ['a1', 'a2', 'a3'] ['a4', 'a5', 'a6'] ['a7']