JavaScript for…in vs for

前端 未结 22 1446
悲哀的现实
悲哀的现实 2020-11-22 07:15

Do you think there is a big difference in for...in and for loops? What kind of \"for\" do you prefer to use and why?

Let\'s say we have an array of associative array

22条回答
  •  失恋的感觉
    2020-11-22 07:33

    Updated answer for 2012 current version of all major browsers - Chrome, Firefox, IE9, Safari and Opera support ES5's native array.forEach.

    Unless you have some reason to support IE8 natively (keeping in mind ES5-shim or Chrome frame can be provided to these users, which will provide a proper JS environment), it's cleaner to simply use the language's proper syntax:

    myArray.forEach(function(item, index) {
        console.log(item, index);
    });
    

    Full documentation for array.forEach() is at MDN.

提交回复
热议问题