var data = {"User1":{"Service1":2,"Service2":1},"User2":{"Service3":1}}
for(var user in data) {
document.write(user + ': ')
for(var service in data[user]){
document.write(service + ': ' + user[service])
}
document.write("<br />")
}
打印:
User1:Service1:undefined Service2:undefined
User2:Service3:undefined
我想要打印
User1:Service1:2 Service2:1
User2:Service3:1
javascript足够还是需要jQuery? 提前谢谢!
答案 0 :(得分:8)
var data = {
User1: {
Service1: 2,
Service2: 1
},
User2: {
Service3: 1
}
};
for (var user in data) {
console.log("User: " + user);
for (var service in data[user]) {
console.log("\tService: " + service + "; value: " + data[user][service]);
}
}
将console.log
替换为document.write
或其他任何内容。
答案 1 :(得分:3)
document.write(service + ': ' + data[user][service])