我需要一个比下面更好的脚本。我无法添加HTML。甚至不能添加“;”记录之间。我还希望能够添加html并轻松列出数据,如:members.Name和members.Expires并对数据进行排序。是否有更流畅,基本的方式来满足我的要求?
我有这个json:
var members = [
{"Name": "Ahmed, Jamshed", "Expires": "2017.10.05"},
{"Name": "Attaya, James", "Expires": "2018.01.12"}
];
和这个脚本:
<script>
var divId = document.getElementById("json")
for(var i=0;i<members.length;i++)
for(var keys in members[i]){
console.log(keys +"-->"+members[i][keys]);
divId.innerHTML = divId.innerHTML + " " +members[i][keys];
}
</script>
答案 0 :(得分:1)
这很好用:更简单,更通用:
<script>
var divId = document.getElementById("json")
string = "";
for(var i=0;i<members.length;i++) {
string += members[i].Name + " " + members[i].Expires +" ● ";
}
divId.innerHTML = string;
</script>