JQuery .each 用法
JQuery .each 用法
對象Array
var array = [1, 2, 3, 4, 5];
$.each(array, function(index, value) {
console.log(index + ": " + value);
});
結果:
0: 1
1: 2
2: 3
3: 4
4: 5
對象Json
var people = [
{ name: "John", age: 30, city: "New York" },
{ name: "Jane", age: 25, city: "Chicago" },
{ name: "Mike", age: 35, city: "Los Angeles" }
];
$.each(people, function(index, person) {
console.log("Name: " + person.name + ", Age: " + person.age + ", City: " + person.city);
});
結果:
Name: John, Age: 30, City: New York
Name: Jane, Age: 25, City: Chicago
Name: Mike, Age: 35, City: Los Angeles
列出JSon每個屬性
var data = {};
data.name = 'Alex';
data.id = "1";
$.each(data, function (key, value) {
alert('key=' + key);
alert('value=' + value);
});
結果:
key=name
value=Alex
留言
張貼留言