2016-03-05 61 views
-5

假設有這樣的數組:我如何循環到這個陣列

var my_array = [{ 
    step: "step1", 
    label: "Item1", 
    price: "99.00" 
}, { 
    step: "step2", 
    label: "Item2", 
    price: "89.00" 
}, { 
    step: "step3", 
    label: "Item3", 
    price: "150.00" 
}] 

我怎樣才能循環到它嗎?

+0

從這裏開始:[MDN學習JavaScript] (https://developer.mozilla.org/en-US/Learn/JavaScript) – 2016-03-05 15:41:43

+0

你有嘗試過什麼嗎? http://stackoverflow.com/help/how-to-ask –

+0

請更清楚地說明您的要求 – Darshan

回答

0

您可以使用for循環:

var array = [{step: "step1", label: "Item1", price: "99.00"}, {step: "step2", label: "Item2", price: "89.00"}, {step: "step3", label: "Item3", price: "150.00"}]; 
for (var i = 0; i < array.length; i++) { 
    var currentItem = array[i]; 
} 

,或者甚至更好,使用.forEach功能:

array.forEach(function(currentItem, index) { 

}); 
0

這是可以循環的方式:

var a = [{ 
    step: "step1", 
    label: "Item1", 
    price: "99.00" 
}, { 
    step: "step2", 
    label: "Item2", 
    price: "89.00" 
}, { 
    step: "step3", 
    label: "Item3", 
    price: "150.00" 
}] 

for (i = 0; i < a.length; i++) { 
    console.log(a[i]); 
} 

工作示例:https://jsfiddle.net/nagcubo2/打開它的控制檯(F12)

screenshot of F12 for the uninitiated

1

只需使用Array.prototype方法,來處理數組內的數據,如map, forEach,或者你甚至可以用for inwhiledo while

var array = [ 
    {step: "step1", label: "Item1", price: "99.00"}, 
    {step: "step2", label: "Item2", price: "89.00"}, 
    {step: "step3", label: "Item3", price: "150.00"} 
] 

array.forEach(function(item){ 
    console.log(item.step, item.label, item.price); 
    // and do what you want 
})