Javascript - While循环变成了无限循环

Javascript - While loop becomes infinite loop?

本文关键字:无限循环 循环 While Javascript      更新时间:2023-09-26

由于某种原因,下面的代码会导致无限循环。为什么会这样呢?

var attributes = responseSC.attrs;
var pos = 0;
//Find The position
while (attributes[pos].name != 'selectLocation' && pos < attributes.length) {
    pos++;
}

你是如何得出这是一个无限循环的结论的?你的页面是否"挂起"或你得到一个"Chrome内存不足"的错误?

是永远不进入循环而不是永远循环的情况吗?

条件永远不满足。

为什么不试试ES6 findIndex ?

attributes.findIndex(function(element) {
  return element.name !== 'selectLocation';
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex