2013-12-11 77 views
0

我想獲取數組內對象的值,即數組中對象的值。

jQuery的

// aim: to alert value of 'm1' for each object (represented as a day) 

myArray = [{"monday":[{"m1":"val1"},{"s1":"val2"},{"m2":"val3"},{"s2":"val4"},{"m3":"val5"},{"s3":"val6"}]},{"tuesday":[{"m1":"val1"},{"s1":"val2"},{"m2":"val3"},{"s2":"val4"},{"m3":"val5"},{"s3":"val6"}]},{"wednesday":[{"m1":"val1"},{"s1":"val2"},{"m2":"val3"},{"s2":"val4"},{"m3":"val5"},{"s3":"val6"}]},{"thursday":[{"m1":"val1"},{"s1":"val2"},{"m2":"val3"},{"s2":"val4"},{"m3":"val5"},{"s3":"val6"}]},{"friday":[{"m1":"val1"},{"s1":"val2"},{"m2":"val3"},{"s2":"val4"},{"m3":"val5"},{"s3":"val6"}]},{"saturday":[{"m1":"val1"},{"s1":"val2"},{"m2":"val3"},{"s2":"val4"},{"m3":"val5"},{"s3":"val6"}]},{"sunday":[{"m1":"val1"},{"s1":"val2"},{"m2":"val3"},{"s2":"val4"},{"m3":"val5"},{"s3":"val6"}]}] 

$.each(myArray, function(k,v) { 
    $.each(k, function(k2,v2) { 
     alert(v2.m1) 
}); 
}); 

的jsfiddle

http://jsfiddle.net/rwone/py8Jz/3/

+0

嵌套循環也許? – Daniel

回答

1

試試這個:http://jsfiddle.net/merrifield69/py8Jz/5/

$.each(myArray, function (k, v) { 
    $.each(v, function (k2, v2) { 
     alert(v2[0].m1) 
    }); 
}); 

如果您只需花一秒鐘思考每個循環中的位置,則很容易就能找出答案。 :]