Javascript $.each() 索引开始编号在 jQuery 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4892282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$.each() index start number in jQuery
提问by user588181
How do I start jQuery $.each()
index from 1 instead of 0?
如何$.each()
从 1 而不是 0开始 jQuery索引?
I am using .each function to populate select box. So here I want to populate options in the select box from 1. So based on a condition I want to add option in first index i.e 0.
我正在使用 .each 函数来填充选择框。所以在这里我想从 1 填充选择框中的选项。所以基于一个条件,我想在第一个索引中添加选项,即 0。
回答by Raynos
So try
所以试试
$.each(obj, function(i, elem) {
if (i === 0) return;
// do stuff
});
Alternatively rewrite your code so its so indexes and data start at 0. JavaScript is interally 0 based rather then 1 based like Matlab and some other languages.
或者重写您的代码,使其索引和数据从 0 开始。JavaScript 本质上是基于 0 的,而不是像 Matlab 和其他一些语言一样基于 1 的。
回答by The Scrum Meister
You can't. Why not just declare another variable at the first line of the function var myIndex = index + 1;
?
你不能。为什么不在函数的第一行声明另一个变量var myIndex = index + 1;
?
回答by jAndy
Not possible. jQuery.each
loops over Objects (Arrays). For real objects, it uses a for in
loop. Objectpropertys don't have a guaranteed index at all by the way.
不可能。jQuery.each
循环对象(数组)。对于真实物体,它使用for in
循环。顺便说一下,对象属性根本没有保证的索引。
For Arrays it uses standard for loop
, but you don't have access to the starting index.
对于 Arrays,它使用 standard for loop
,但您无权访问起始索引。
Your best shot is to just skip the first element.
最好的办法是跳过第一个元素。
$.each(obj, function(index, elem) {
if( index === 0 )
return true;
// code
});
回答by s3c
You can start at any given starting value by simply adding that number to the current loop-set index value.
您可以从任何给定的起始值开始,只需将该数字添加到当前循环集索引值即可。
$.each(( index ) => {
index += 1 //or any other starting value
// your code
});
回答by Bashirpour
var data = [42, 1337];
$.each(data, function(i) {
i++;
console.log(i);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
回答by CharlotteOswald
This is what I did:
这就是我所做的:
var start = index + 1;
This just adds 1 to the current index.. then use that new variable instead of 'index'.
这只是将 1 添加到当前索引..然后使用该新变量而不是“索引”。
回答by David Tang
Seeing the clarification points in the comments below, jQuery.each
or some variation thereof isn't the right tool at all. Use a for loop, and add 1 to the index to output the value of the <option>
.
在下面的评论中看到澄清点,jQuery.each
或者其中的一些变体根本不是正确的工具。使用 for 循环,将索引加 1 以输出<option>
.
Use .prepend
to insert an option
as the first child of a select
:
使用.prepend
插入一个option
作为的第一个孩子select
:
for (var i = 0; i < 10; i++) {
$('select').prepend('<option>' + (i + 1) + '</option>');
}
回答by David Huts
$('.article_number').each(function(index) {
index=index+1;
$(this).text(index);
});
even starting from 0 each time it takes the index and adds one for the text.
甚至每次从 0 开始获取索引并为文本添加 1。
so 0 will become 1 in text. 1 will become 2 in text and so on.
所以文本中的 0 将变为 1。1 将在文本中变为 2,依此类推。
This won't interrupt the last item in line cause it's only changing the index of the text not on the each function.
这不会中断行中的最后一项,因为它只会更改文本的索引,而不是在每个函数上。
Hope this is what you ment.
希望这是你的心理。