for ... in循环和jQuery each()函数有什么区别? [重复]

时间:2022-12-08 19:52:50

I am using the following scripts to iterate the object (I don't know which one is best to use, please tell me which one is the best):

我使用以下脚本来迭代对象(我不知道哪一个最好用,请告诉我哪一个是最好的):

var days = {Sunday: 0, Monday: 1, Tuesday: 2, Wednesday: 3, Thursday: 4, Friday: 5, Saturday: 6};

$.each(days, function(key, value){
    $('#days').append('<li>' + key + '(' + value + ')</li>');
});


for(var key in days){
    $('#days').append('<li>' + key + '(' + days[key] + ')</li>');
}

3 个解决方案

#1


4  

Either way, you should be caching that selector:

无论哪种方式,你应该缓存该选择器:

var elem = $( '#days' );
$.each(days,function(key,value){
    elem.append('<li>'+key+'('+value+')</li>');
});

.each() would be better in this case. The pattern is cleaner.

在这种情况下,.each()会更好。图案更清洁。

With a for loop, you'd want to use obj.hasOwnProperty(key) so that you don't dredge through inherited properties... Which adds another layer of indenting:

使用for循环,您需要使用obj.hasOwnProperty(key),这样您就不会通过继承的属性进行挖掘...这会添加另一层缩进:

var elem = $( '#days' );
for( var key in days ){
  if( days.hasOwnProperty( key ) ){
    elem.append('<li>'+key+'('+days[key]+')</li>');
  }
}

#2


2  

It is clear without any performance tests that native javascript for loop is faster, but there is no big difference for small arrays like 10-20 small items. So use whichever you want.

很明显,如果没有任何性能测试,原生javascript for循环会更快,但对于像10-20个小项目这样的小型数组没有太大区别。所以使用你想要的任何一个。

#3


0  

Features of $.each: It will stop looping if the function returns false. It works for both objects and arrays. $(this) refers to the current element being processed.

$ .each的功能:如果函数返回false,它将停止循环。它适用于对象和数组。 $(this)指的是当前正在处理的元素。

Drawbacks of $.each: There's a little more overhead, as it does some initial checking of arguments and has to perform a function call for each element.

$ .each的缺点:有一些开销,因为它对参数进行了初步检查,并且必须对每个元素执行函数调用。

But as others said, the overhead is likely to be negligible unless you're processing a large number of items.

但正如其他人所说,除非你处理大量物品,否则开销可能微不足道。

One other benefit of $.each() is simply the easy recognition of the word -- having it at the beginning of a statement makes it obvious that you're looping over a collection. It's also easy to search for.

$ .each()的另一个好处就是对单词的轻松识别 - 在语句的开头加上它可以明显地表明你正在循环一个集合。它也很容易搜索。

#1


4  

Either way, you should be caching that selector:

无论哪种方式,你应该缓存该选择器:

var elem = $( '#days' );
$.each(days,function(key,value){
    elem.append('<li>'+key+'('+value+')</li>');
});

.each() would be better in this case. The pattern is cleaner.

在这种情况下,.each()会更好。图案更清洁。

With a for loop, you'd want to use obj.hasOwnProperty(key) so that you don't dredge through inherited properties... Which adds another layer of indenting:

使用for循环,您需要使用obj.hasOwnProperty(key),这样您就不会通过继承的属性进行挖掘...这会添加另一层缩进:

var elem = $( '#days' );
for( var key in days ){
  if( days.hasOwnProperty( key ) ){
    elem.append('<li>'+key+'('+days[key]+')</li>');
  }
}

#2


2  

It is clear without any performance tests that native javascript for loop is faster, but there is no big difference for small arrays like 10-20 small items. So use whichever you want.

很明显,如果没有任何性能测试,原生javascript for循环会更快,但对于像10-20个小项目这样的小型数组没有太大区别。所以使用你想要的任何一个。

#3


0  

Features of $.each: It will stop looping if the function returns false. It works for both objects and arrays. $(this) refers to the current element being processed.

$ .each的功能:如果函数返回false,它将停止循环。它适用于对象和数组。 $(this)指的是当前正在处理的元素。

Drawbacks of $.each: There's a little more overhead, as it does some initial checking of arguments and has to perform a function call for each element.

$ .each的缺点:有一些开销,因为它对参数进行了初步检查,并且必须对每个元素执行函数调用。

But as others said, the overhead is likely to be negligible unless you're processing a large number of items.

但正如其他人所说,除非你处理大量物品,否则开销可能微不足道。

One other benefit of $.each() is simply the easy recognition of the word -- having it at the beginning of a statement makes it obvious that you're looping over a collection. It's also easy to search for.

$ .each()的另一个好处就是对单词的轻松识别 - 在语句的开头加上它可以明显地表明你正在循环一个集合。它也很容易搜索。