<script src="js/jquery-1.12.3.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
$(function(){
var names = ["Jack","Tom","Mary","Fiona"];
$.each(names,function(idx,value){console.log("陣列:"+value)})
var emp = {"name":"Jack","salary":32000,"workYears":2};
$.each(emp,function(keys,value){console.log("物件:"+keys+":"+value)})
var emps = [{"name":"Jack","salary":32000,"workYears":2},
{"name":"Mary","salary":42000,"workYears":5},
{"name":"Tom","salary":52000,"workYears":7}];
$.each(emps,function(index,values){
//取出陣列中的一個個物件:[{}{}{}]→{}{}{}
console.log("陣列中有物件"+values.name+":"+values.salary+":"+values.workYears)
})
$.each(emps,function(index,valuess){
//取出陣列中的一個個物件:[{}{}{}]→{}{}{}
$.each(valuess,function(keys,value){console.log("陣列中有物件2:"+keys+":"+value)})
})
var data = {
emps: [{ name: "Jack", age: 29, contact: {home:"02-12345678",cell:"0911222333"}},
{ name: "Mary", age: 35, contact: { home: "02-33116745", cell: "0932112654" } },
{ name: "Tom", age: 26, contact: { home: "02-54346666", cell: "0955832123" } }]
};
//data--{}
//emps--[]
$.each(data,function(name,values){
//進入data中,有一個物件emps[],emps裡面有三個物件{},也就是[{},{},{}]
//接下來從陣列中取出一個個物件
$.each(values,function(idx,value){
//value就是一個個物件{}{}{}
console.log(value.name)
console.log(value.age)
console.log(value.contact.home)
console.log(value.contact.cell)
})
})
$.each(data.emps, function(index, value) {
console.log(value.name)
console.log(value.age)
console.log(value.contact.home)
console.log(value.contact.cell)
})
})
</script>
留言列表