问题描述
var remSize = []我使用这个JavaScript遍历一个数组并找到一个匹配的数组元素。 ,
szString,remData,remIndex,i;
for(i = 0; i< remSize.length; i ++){
//我正在寻找索引i,当条件为真
remSize [我] .size == remData.size? remIndex = i:remIndex = -1;
$数组包含这些大小: [ 34,36,38...]
。
remData.size
是我正在查找的大小(例如36如果我正在搜索的大小是在索引中,我需要返回索引我
>
否则,我需要返回 -1
。有没有更好的方法来做到这一点?
解决方案 停止为
在JavaScript的早期循环,你使用:
var remSize = [],
szString,
remData,
remIndex ,
i;
/ * ...我假设有代码在这里把条目放在`remSize`中,并把一些东西分配给`remData` ... * /
remIndex = -1; //设置一个默认值,如果我们没有找到它
for(i = 0; i< remSize.length; i ++){
//我正在寻找索引i,当条件是真的
if(remSize [i] .size === remData.size){
remIndex = i;
break; //< ===早早跳出循环
code $
特定的用例,你可以使用 Array#findIndex
这个条目的索引)或 Array#find
(找到条目本身),两者都可以被填充/填充: var remSize = [],
szString,
remData,
remIndex;
/ * ...我假设这里有一些代码将条目放在`remSize`中,并将其分配给`remData` ... * /
remIndex = remSize.findIndex函数(entry){
return entry.size === remData.size;
});
Array#find
:
var remSize = [],
szString,
remData,
remEntry;
/ * ...我假设有代码在这里把条目放在`remSize`中,并把一些东西分配给`remData` ... * /
remEntry = remSize.find函数(entry){
return entry.size === remData.size;
});
Array#findIndex
第一次停回调返回一个真值,将该呼叫的索引返回给回调;它返回 -1
,如果回调永远不会返回一个真值。 Array#find
当它找到你要找的东西时也停下来,但是它返回的是条目,而不是它的索引(或者 undefined $ c
如果您使用的是ES5兼容环境(或ES5垫片),则可以使用新的对阵列上的,它调用回调,直到回调函数返回一个真值:
$ p $ var $ rem $ = $ b $ szString
remData,
remIndex;
/ * ...我假设有代码在这里把条目放在`remSize`中,并把一些东西分配给`remData` ... * /
remIndex = -1; //< ==设置一个默认值,如果我们没有找到它
remSize.some(function(entry,index){
if(entry.size === remData.size){
remIndex = index;
return true; //< ==对于`Array#some`
}
}};
如果您使用jQuery,您可以使用循环访问数组;看起来像这样:
var remSize = [],
szString,
remData,
remIndex;
/ * ...我假设有代码在这里把条目放在`remSize`中,并把一些东西分配给`remData` ... * /
remIndex = -1; //< ==设置默认值,如果我们没有找到它
jQuery.each(remSize,function(index,entry){
if(entry.size === remData.size){
remIndex = index;
return false; //< == break对于jQuery.each
}
}
I'm using this JavaScript to iterate through an array and find a matching array element:
var remSize = [],
szString, remData, remIndex, i;
for (i = 0; i < remSize.length; i++) {
// I'm looking for the index i, when the condition is true
remSize[i].size == remData.size ? remIndex = i : remIndex = -1;
}
The array contains these "sizes": ["34", "36", "38"...]
.
remData.size
is the "size" I'm looking for (e.g. "36").
I need to return the index i
if the size I'm searching for is in the index. Otherwise I need to return -1
. Is there a better way to do this?
解决方案 To stop a for
loop early in JavaScript, you use break
:
var remSize = [],
szString,
remData,
remIndex,
i;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remIndex = -1; // Set a default if we don't find it
for (i = 0; i < remSize.length; i++) {
// I'm looking for the index i, when the condition is true
if (remSize[i].size === remData.size) {
remIndex = i;
break; // <=== breaks out of the loop early
}
}
If you're in an ES2015 (aka ES6) environment, for this specific use case, you can use Array#findIndex
(to find the entry's index) or Array#find
(to find the entry itself), both of which can be shimmed/polyfilled:
var remSize = [],
szString,
remData,
remIndex;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remIndex = remSize.findIndex(function(entry) {
return entry.size === remData.size;
});
Array#find
:
var remSize = [],
szString,
remData,
remEntry;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remEntry = remSize.find(function(entry) {
return entry.size === remData.size;
});
Array#findIndex
stops the first time the callback returns a truthy value, returning the index for that call to the callback; it returns -1
if the callback never returns a truthy value. Array#find
also stops when it finds what you're looking for, but it returns the entry, not its index (or undefined
if the callback never returns a truthy value).
If you're using an ES5-compatible environment (or an ES5 shim), you can use the new some
function on arrays, which calls a callback until the callback returns a truthy value:
var remSize = [],
szString,
remData,
remIndex;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remIndex = -1; // <== Set a default if we don't find it
remSize.some(function(entry, index) {
if (entry.size === remData.size) {
remIndex = index;
return true; // <== Equivalent of break for `Array#some`
}
});
If you're using jQuery, you can use jQuery.each
to loop through an array; that would look like this:
var remSize = [],
szString,
remData,
remIndex;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remIndex = -1; // <== Set a default if we don't find it
jQuery.each(remSize, function(index, entry) {
if (entry.size === remData.size) {
remIndex = index;
return false; // <== Equivalent of break for jQuery.each
}
});
这篇关于如何停止JavaScript for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-12 05:00