如何 Return 数组数组中的下一个值
How to Return a next value from an array of array
我需要在每个函数调用中迭代数组的数组中的下一个值。
forex: 如果我有一个array
const Arr = [ ['1','2', '3'], ['11','12', '13'], ['11','22', '33'],]
我有一个功能
getNumber(id: number): string {
let n;
for(const i in Arr) {
for(const j in Arr[i]{
n = j;
}
}
return n;
}
我在这里需要多次调用该函数,但每次都应该 return 下一个数字
let i = 4;
for(let j=0; j<=i; j++) {
const g = {
number: this.getNumber(i); //this i means id
}
}
调用函数后,会return像
'1'
'2'
'3'
'11'
'12'
请帮我解决这个问题
您可以在每次调用该函数时使用 generator 获取下一个值。
要将多维数组转换为平面数组,您可以使用 Array.flat() 方法。
const Arr = [ ['1','2', '3'], ['11','12', '13'], ['11','22', '33'],];
function* getNumber(arr) {
for(const num of arr) {
yield num;
}
}
const gen = getNumber(Arr.flat());
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
您可以使用内置的数组迭代器,该迭代器使用@Reyno 的答案中所示的生成器。它只是更简单,您不必编写那么多代码,因为它已经为您编写好了。
const arr = [[1],[2],[3],[4]];
const it = arr.flat()[Symbol.iterator]();
it.next() // { value: 1, done: false }
it.next() // { value: 2, done: false }
it.next() // { value: 3, done: false }
it.next() // { value: 4, done: false }
it.next() // { value: undefined, done: true }
此外,如果你不想使用生成器和迭代器,因为它们来自 ES6,你可以只写一个函数:
function getIterator(arr) {
var counter = 0;
return function next() {
if (counter === arr.length) { return undefined; }
return arr[counter++];
};
}
var arr = [1, 2, 3, 4];
var next = getIterator(arr);
next(); // 1
next(); // 2
next(); // 3
next(); // 4
next(); // undefined
next(); // undefined
arr.push(5);
next(); // 5
顺便说一句,即使在 ES6+ 中,这也可能是一个更好的解决方案,因为您将能够将元素推送到数组中,并且仍然能够迭代新元素。 iterator/generator 解决方案在到达数组末尾后关闭,因此您必须创建一个新的解决方案才能再次迭代。
我需要在每个函数调用中迭代数组的数组中的下一个值。
forex: 如果我有一个array
const Arr = [ ['1','2', '3'], ['11','12', '13'], ['11','22', '33'],]
我有一个功能
getNumber(id: number): string {
let n;
for(const i in Arr) {
for(const j in Arr[i]{
n = j;
}
}
return n;
}
我在这里需要多次调用该函数,但每次都应该 return 下一个数字
let i = 4;
for(let j=0; j<=i; j++) {
const g = {
number: this.getNumber(i); //this i means id
}
}
调用函数后,会return像
'1'
'2'
'3'
'11'
'12'
请帮我解决这个问题
您可以在每次调用该函数时使用 generator 获取下一个值。
要将多维数组转换为平面数组,您可以使用 Array.flat() 方法。
const Arr = [ ['1','2', '3'], ['11','12', '13'], ['11','22', '33'],];
function* getNumber(arr) {
for(const num of arr) {
yield num;
}
}
const gen = getNumber(Arr.flat());
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
您可以使用内置的数组迭代器,该迭代器使用@Reyno 的答案中所示的生成器。它只是更简单,您不必编写那么多代码,因为它已经为您编写好了。
const arr = [[1],[2],[3],[4]];
const it = arr.flat()[Symbol.iterator]();
it.next() // { value: 1, done: false }
it.next() // { value: 2, done: false }
it.next() // { value: 3, done: false }
it.next() // { value: 4, done: false }
it.next() // { value: undefined, done: true }
此外,如果你不想使用生成器和迭代器,因为它们来自 ES6,你可以只写一个函数:
function getIterator(arr) {
var counter = 0;
return function next() {
if (counter === arr.length) { return undefined; }
return arr[counter++];
};
}
var arr = [1, 2, 3, 4];
var next = getIterator(arr);
next(); // 1
next(); // 2
next(); // 3
next(); // 4
next(); // undefined
next(); // undefined
arr.push(5);
next(); // 5
顺便说一句,即使在 ES6+ 中,这也可能是一个更好的解决方案,因为您将能够将元素推送到数组中,并且仍然能够迭代新元素。 iterator/generator 解决方案在到达数组末尾后关闭,因此您必须创建一个新的解决方案才能再次迭代。