我有以下javascript对象:
result = {
"banking6dig":{
"GM-B-001":{
"releaseDate":"2/2/2012 14:44","noOfHex":"3","versInfo":"6 digit Banking"
},
"GM-B-002":{
"releaseDate":"1/2/2012 14:46","noOfHex":"3","versInfo":"6 digit Banking with changes"
}
},
"paynpark":[]
}
说明:
“banking6dig”和“paynpark”是申请书 “banking6dig”有两个颠覆:“GM-B-001”和“GM-B-002” “paynpark”没有颠覆。
此外,每个subversion都有自己的属性,即“releaseDate”,“noOfHex”和“versInfo”。
此对象“结果”是在php请求之后构建的,因此它可以包含任意数量的应用和颠覆;但格式总是一样的。
我在jsFiddle上试过这个:http://jsfiddle.net/2JLtZ/1/
答案 0 :(得分:1)
1
var i=0;
for (var j in result['banking6dig']) i++;
alert('banking6dig has '+i+' subversions'):
2
result.electricity={}
3
paynpark.fixedRate={};
感谢AlienWebguy指出这一点。
4
result['banking6dig']["GM-B-001"]["releaseDate"]="3/12/2012 14:46";
答案 1 :(得分:1)
result = {"banking6dig":{"GM-B-001":{"releaseDate":"2/2/2012 14:44","noOfHex":"3","versInfo":"6 digit Banking"},"GM-B-002":{"releaseDate":"1/2/2012 14:46","noOfHex":"3","versInfo":"6 digit Banking with changes"}},"paynpark":[]};
var app = [],
svn = {};
for (var _app in result) {
if(result.hasOwnProperty(_app)){
app.push(_app);
svn[_app] = [];
for (var _svn in result[_app]){
if(result[_app].hasOwnProperty(_svn)){
svn[_app].push(_svn);
}
}
}
}
// How man apps?
alert(app.length);
// How many subversions?
alert(svn['banking6dig'].length);
// Add an app 'electricity'
result.electricity = {};
// Add subversion to paynpark
result.paynpark.fixedRate = {}
// Add electricity
result.electricity = {};
app.push('electricity');
// Modify subversion
result.banking6dig['GM-B-001'].releaseDate = '3/12/2012';
console.log(result);