var obj = {"a": 1,"b": 2,"c": 3};
for (var prop in obj) {if (obj.hasOwnProperty(prop)) {// or if (Object.prototype.hasOwnProperty.call(obj,prop)) for safety...console.log("prop: " + prop + " value: " + obj[prop])}}
var i = 0,item;
// Note this is weak to sparse arrays or falsey valuesfor ( ; item = myStringArray[i++] ; ){item; // This is the string at the index.}
或者如果你真的想得到id并有一个非常经典的for循环:
var i = 0,len = myStringArray.length; // Cache the length
for ( ; i < len ; i++ ){myStringArray[i]; // Don't use this if you plan on changing the length of the array}
// REQUIRES ECMASCRIPT 2015+var s, myStringArray = ["Hello", "World"];for (s of myStringArray) {// ... do something with s ...}
或者更好的是,因为ECMAScript 2015也提供了块范围的变量:
// REQUIRES ECMASCRIPT 2015+const myStringArray = ["Hello", "World"];for (const s of myStringArray) {// ... do something with s ...}// s is no longer defined here
var i, s, myStringArray = [ "Hello", "World" ], len = myStringArray.length;for (i=0; i<len; ++i) {if (i in myStringArray) {s = myStringArray[i];// ... do something with s ...}}
var myStringArray = ['Hello', 'World']; // The array uses [] not {}for (var i in myStringArray) {console.log(i + ' -> ' + myStringArray[i]); // i is the index/key, not the item}
for (var i=0, item; item=someArray[i]; i++) {// item is "some", then "example", then "array"// i is the index of item in the arrayalert("someArray[" + i + "]: " + item);}
<script src="//code.jquery.com/jquery-2.1.0.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script><script>Benchmark.prototype.setup = function() {// Fake function with minimal action on the valuevar tmp = 0;var process = function(value) {tmp = value; // Hold a reference to the variable (prevent engine optimisation?)};
// Declare the test Arrayvar arr = [];for (var i = 0; i < 100000; i++)arr[i] = i;};</script>
测试:
<a href="http://jsperf.com/native-loop-performance/16"title="http://jsperf.com/native-loop-performance/16"><img src="http://i.imgur.com/YTrO68E.png" title="Hosted by imgur.com" /></a>
var myArray = [3, 5, 7];myArray.foo = "hello";
for (var i in myArray) {console.log(i); // logs 0, 1, 2, "foo"}
for (var i of myArray) {console.log(i); // logs 3, 5, 7}
此外,您需要考虑没有Internet Explorer版本支持for...of(边缘12+支持),并且for...in至少需要Internet Explorer 10。
function A(a) {let r=0;while(a.length) r+= a.shift().length;return r;}
function B(a) {let r=0;for(i in a) r+= a[i].length;return r;}
function C(a) {let r=0;for(x of a) r+= x.length;return r;}
function D(a) {let r=0;for (i=0; i<a.length; ++i) r+= a[i].length;return r;
}
function E(a) {let r=0;a.forEach(x=> r+= x.length);return r;}
let arr= ["Hello", "World!"];[A,B,C,D,E].forEach(f => console.log(`${f.name}: ${f([...arr])}`))
// 1: for
for (let i = 0; i < arr.length; ++i) {console.log(arr[i]);}
// 2: forEach
arr.forEach((v, i) => console.log(v));
// 3: for in
for (let i in arr) {console.log(arr[i]);}
// 4: for of
for (const v of arr) {console.log(v);}
/**array is the array your wish to iterate.response is what you want to return.index increments each time the function calls itself.**/
const iterateArray = (array = [], response = [], index = 0) => {const data = array[index]
// If this condition is met. The function returns and stops calling itself.if (!data) {return response}// Do work...response.push("String 1")response.push("String 2")
// Do more work...
// THE FUNCTION CALLS ITSELFiterateArray(data, response, index+=1)}
const mainFunction = () => {const text = ["qwerty", "poiuyt", "zxcvb"]
// Call the recursive functionconst finalText = iterateArray(text)console.log("Final Text: ", finalText()}