寻找for
和for...in
循环的最后一次迭代有多种方法。但是我如何在for...of
循环中找到最后一次迭代?我在文档中找不到相关信息。
for (item of array) {
if (detect_last_iteration_here) {
do_not_do_something
}
}
寻找for
和for...in
循环的最后一次迭代有多种方法。但是我如何在for...of
循环中找到最后一次迭代?我在文档中找不到相关信息。
for (item of array) {
if (detect_last_iteration_here) {
do_not_do_something
}
}
一种方法是使用Array.prototype.entries():
for (const [i, value] of arr.entries()) {
if (i === arr.length - 1) {
// do your thing
}
}
另一种方法是像Shidersz建议的那样,在循环外部保持一个计数。我认为您不希望检查 indexOf(item)
,因为如果最后一个项在数组中的其他位置重复出现,则会造成问题...
一种可能的方法是在循环外初始化一个计数器,并在每次迭代时将其减少:
const data = [1, 2, 3];
let iterations = data.length;
for (item of data)
{
if (!--iterations)
console.log(item + " => This is the last iteration...");
else
console.log(item);
}
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
注意!--iterations
被解析为!(--iterations)
,当iterations=1
时,表达式将为true
。
var array = [1, 2, 3],
item;
for (item of array.slice(0, -1)) {
console.log(item)
}
在ES6中,通过调用数组的entries()方法,你可以做到这一点 =>
const array = [1, 2, 3];
for (const [i, v] of array.entries()) {
//Handled last iteration
if( i === array.length-1) {
continue;
}
console.log(i, v)// it will print index and value
}
function* withLast(iterable) {
const iterator = iterable[Symbol.iterator]();
let curr = iterator.next();
let next = iterator.next();
while (!curr.done) {
yield [next.done, curr.value];
[curr, next] = [next, iterator.next()];
}
}
next
被调用了两次。在随后的每一次迭代中,next
只被调用一次,即针对与当前迭代相应的下一步元素。function* withLast(iterable) {
const iterator = iterable[Symbol.iterator]();
let curr = iterator.next();
let next = iterator.next();
while (!curr.done) {
yield [next.done, curr.value];
[curr, next] = [next, iterator.next()];
}
}
const arr = [1, 2, 3];
for (const [isLast, element] of withLast(arr)) {
console.log(`${element} ${isLast ? 'is' : 'is not'} the last.`);
}
function* withLast(iterable) {
const iterator = iterable[Symbol.iterator]();
let curr = iterator.next();
let next = iterator.next();
while (!curr.done) {
yield [next.done, curr.value];
[curr, next] = [next, iterator.next()];
}
}
// Generator function that creates a "generic" iterator:
function* gen() {
yield 'a';
yield 'b';
yield 'c';
}
for (const [isLast, element] of withLast(gen())) {
console.log(`${element} ${isLast ? 'is' : 'is not'} the last.`);
}
for (item of array.slice(0, -1)) {
//do something for every element except last
}
似乎在for..of的规范中没有找到任何内容。
有两个解决方法:
如果可以的话,只需将一个标记推到数组末尾并在该标记上进行操作,就像这样:
myArray.push('FIN')
for (el of myArray){
if (el === 'FIN')
//ending code
}
Array.prototype.at()
和-1
。Array.prototype.at()
的MDN。for (item of array) {
if (item === array.at(-1)) {
// do_not_do_something
}
}
const arr = ["verb", "noun", "pronoun"];
for (let item of arr) {
if (arr.length -1 !== arr.indexOf(item)) {
console.log('With Commas');
} else {
console.log('No Commars');
}
}
lastIndexOf
,即使数组中有重复项,它仍将失败。 - General Grievanceconst chain = ['ABC', 'BCA', 'CBA'];
let findOut;
for (const lastIter of chain) {
findOut = lastIter; //Last iteration value stored in each iteration.
}
console.log(findOut);