我想用给定次数的扩展语法填充数组 [ ]
I want to populate an array [ ] , with a spread syntax by a given number of times
所以我有一个可变大小的变量,它计算一个参数中传递了多少个数组。
concatenate(...arg){
let size = arg.length ;
}
我想使用与该大小相等的次数的扩展语法,更具体地说我想连接所有数组,例如:
let arr1 = [1,2,3];
let arr2 = [3,2,1];
let arr3 = [4,5,6];
// unknown number of array
let finalArr = [...arr1,...arr2,...arr3,...etc];
所以我想问你的是用一个可以为我处理的模板替换硬编码的 finalArr?
到目前为止我尝试了什么:
// Arrays share the same content,ex : Arr : {content:[Arr(3)}
function concatenate(...arg){
let size = arg.length;
let arrays = arg;
let tmplt = Arr(size+1).join(`Arr${size}`);
let finalArr = [];
while(size>=0){
finalArr= [tmplt];
size--;
}
return finalArr;
}
//input
let var1 = [{content:[2,9,10]}];
let var2 = [{content:[3,1,3]}];
let var3 = [{content:[9,1,8]}];
var1.concatenate(var2,var3);
// expected output
[2,9,10,3,1,3,9,1,8]
最后,我尝试在我制作的 class 列表中重现 Array 的 concat() 方法。规则是:不允许使用 Array.prototype.methods()。
到目前为止的完整代码:
class List {
constructor(items){
this.content = !items ? [] : items;
this.size = this.length();
}
length(arg=0){
let size = arg;
if(!this.content[arg]){
return size;
}
size++;
return this.length(size);
}
append(arg){
if(this.size ==0 && this.size == arg.size){
this.content = new List();
return this.content;
}
this.content = [...this.content,...arg.content]
return this.content;
}
concatenate(...arg){
let test = arg;
let size = 0;
let final = [];
while(test[size]!=undefined){
// final += [...test[size].content];
size++;
let x = Array(size+1).join(`...test${size}.content`);
while(size>=0){
size--;
}
}
}
}
let aList = new List([2,9,10]);
let anotherList = new List([3,14,1]);
let thirdList = new List([9,0,8]);
aList.concatenate(anotherList,thirdList);
您可以使用 a generator function which takes arrays delegates to each of the iterators。这允许您根据需要展平任意数量的数组,并且可以与扩展符号一起使用:
function* concat(...arrays) {
for (const array of arrays)
yield* array;
}
function concatenate(...arg){
return [...concat(...arg)];
}
let arr1 = [1,2,3];
let arr2 = [3,2,1];
let arr3 = [4,5,6];
let finalArr = concatenate(arr1, arr2, arr3);
console.log(finalArr);
.as-console-wrapper { max-height: 100% !important; }
您可以使用嵌套的 for 循环。
function concatenate(...args) {
const result = [];
for (const array of args) for (const { content } of array) result.push(...content);
return result;
}
const
var1 = [{ content: [2, 9, 10] }],
var2 = [{ content: [3, 1, 3] }],
var3 = [{ content: [9, 1, 8] }];
console.log(...concatenate(var1, var2, var3)); // [2, 9, 10, 3, 1, 3, 9, 1, 8]
您可以对数组使用 .push 方法来连接它们。
let arr1 = [1,2,3];
let arr2 = [3,2,1];
let arr3 = [4,5,6];
function concatenate(...arg){
let size = arg.length ;
let finalArr = [];
for(let i = 0; i < size; i++)
finalArr.push(...arg[i]);
return finalArr
}
finalArr = concatenate(arr1, arr2, arr3);
console.log(finalArr);
所以我有一个可变大小的变量,它计算一个参数中传递了多少个数组。
concatenate(...arg){
let size = arg.length ;
}
我想使用与该大小相等的次数的扩展语法,更具体地说我想连接所有数组,例如:
let arr1 = [1,2,3];
let arr2 = [3,2,1];
let arr3 = [4,5,6];
// unknown number of array
let finalArr = [...arr1,...arr2,...arr3,...etc];
所以我想问你的是用一个可以为我处理的模板替换硬编码的 finalArr?
到目前为止我尝试了什么:
// Arrays share the same content,ex : Arr : {content:[Arr(3)}
function concatenate(...arg){
let size = arg.length;
let arrays = arg;
let tmplt = Arr(size+1).join(`Arr${size}`);
let finalArr = [];
while(size>=0){
finalArr= [tmplt];
size--;
}
return finalArr;
}
//input
let var1 = [{content:[2,9,10]}];
let var2 = [{content:[3,1,3]}];
let var3 = [{content:[9,1,8]}];
var1.concatenate(var2,var3);
// expected output
[2,9,10,3,1,3,9,1,8]
最后,我尝试在我制作的 class 列表中重现 Array 的 concat() 方法。规则是:不允许使用 Array.prototype.methods()。 到目前为止的完整代码:
class List {
constructor(items){
this.content = !items ? [] : items;
this.size = this.length();
}
length(arg=0){
let size = arg;
if(!this.content[arg]){
return size;
}
size++;
return this.length(size);
}
append(arg){
if(this.size ==0 && this.size == arg.size){
this.content = new List();
return this.content;
}
this.content = [...this.content,...arg.content]
return this.content;
}
concatenate(...arg){
let test = arg;
let size = 0;
let final = [];
while(test[size]!=undefined){
// final += [...test[size].content];
size++;
let x = Array(size+1).join(`...test${size}.content`);
while(size>=0){
size--;
}
}
}
}
let aList = new List([2,9,10]);
let anotherList = new List([3,14,1]);
let thirdList = new List([9,0,8]);
aList.concatenate(anotherList,thirdList);
您可以使用 a generator function which takes arrays delegates to each of the iterators。这允许您根据需要展平任意数量的数组,并且可以与扩展符号一起使用:
function* concat(...arrays) {
for (const array of arrays)
yield* array;
}
function concatenate(...arg){
return [...concat(...arg)];
}
let arr1 = [1,2,3];
let arr2 = [3,2,1];
let arr3 = [4,5,6];
let finalArr = concatenate(arr1, arr2, arr3);
console.log(finalArr);
.as-console-wrapper { max-height: 100% !important; }
您可以使用嵌套的 for 循环。
function concatenate(...args) {
const result = [];
for (const array of args) for (const { content } of array) result.push(...content);
return result;
}
const
var1 = [{ content: [2, 9, 10] }],
var2 = [{ content: [3, 1, 3] }],
var3 = [{ content: [9, 1, 8] }];
console.log(...concatenate(var1, var2, var3)); // [2, 9, 10, 3, 1, 3, 9, 1, 8]
您可以对数组使用 .push 方法来连接它们。
let arr1 = [1,2,3];
let arr2 = [3,2,1];
let arr3 = [4,5,6];
function concatenate(...arg){
let size = arg.length ;
let finalArr = [];
for(let i = 0; i < size; i++)
finalArr.push(...arg[i]);
return finalArr
}
finalArr = concatenate(arr1, arr2, arr3);
console.log(finalArr);