在我的JSON文件中,我有一个对象,包含多个对象的数组。如果对象的值等于某个数字,我想调用一个函数。如果相等,那么仅具有该值的对象将被输入到所选的div中。最好的方法是什么?
示例:
我有一个"个人资料"对象并想知道对象"类别"等于数字" 1"。如果是,请将这些对象输入到第一个" .contentBox .viewed"。如果"类别" object等于数字" 2",将这些对象输入到第二个" .contentBox .viewed"。
JSON数据:
{
"profiles": [
{ "firstName" : "John", "lastName" : "Doe", "gender": "male", "category": 1 },
{ "firstName" : "Jane", "lastName" : "Austen", "gender": "female", "category": 1 },
{ "firstName" : "Alexander", "lastName" : "Beth", "gender": "male", "category": 1 },
{ "firstName" : "Sarah", "lastName" : "Kelly", "gender": "female", "category": 2 },
{ "firstName" : "Rachel", "lastName" : "Haiworth", "gender": "female", "category": 2 },
{ "firstName" : "Thomas", "lastName" : "Alfae", "gender": "male", "category": 2 },
{ "firstName" : "William", "lastName" : "Lee", "gender": "male", "category": 2 }
]
}
当前JS的样本:
xhr.onload = function() {
if (xhr.status === 200) {
responseObject = JSON.parse(xhr.responseText);
responseObject.profiles.sort(function(a, b) {
return SortAlphabetically(a.firstName, b.firstName);
});
var newViewers = '';
for (var i = 0; i < responseObject.profiles.length; i++) {
newViewers += '<span class="user">' + responseObject.profiles[i].firstName + ' ';
newViewers += responseObject.profiles[i].lastName + '</span>';
newViewers += ' ';
}
//Update Page With New Content
var viewerSection = $('div.viewed');
viewerSection.html(newViewers);
checkViewers();
}
};
xhr.open('GET', 'data.json', true);
xhr.send(null);
我不确定是否有类似于"profiles.category.val() === 1
&#34;可能有用或者有更好的路线。
查看当前和完整的Plunker。
答案 0 :(得分:2)
我为您做了一个非常基本的示例,根据类别将名称加载到表的列中。
var json = {
"profiles": [
{ "firstName" : "John", "lastName" : "Doe", "gender": "male", "category": 1 },
{ "firstName" : "Jane", "lastName" : "Austen", "gender": "female", "category": 1 },
{ "firstName" : "Alexander", "lastName" : "Beth", "gender": "male", "category": 1 },
{ "firstName" : "Sarah", "lastName" : "Kelly", "gender": "female", "category": 2 },
{ "firstName" : "Rachel", "lastName" : "Haiworth", "gender": "female", "category": 2 },
{ "firstName" : "Thomas", "lastName" : "Alfae", "gender": "male", "category": 2 },
{ "firstName" : "William", "lastName" : "Lee", "gender": "male", "category": 2 }
]
}
for (var i = 0; i < json.profiles.length; i++) {
var obj = json.profiles[i];
if (obj.category == 1) {
$("#content").append("<tr><td>" + obj.firstName + " " + obj.lastName + "</td><td></td></tr>");
}
else if (obj.category == 2) {
$("#content").append("<tr><td></td><td>" + obj.firstName + " " + obj.lastName + "</td></tr>");
}
}
&#13;
table, tr, td, th{border: 1px solid black; border-collapse:collapse;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Category 1</th>
<th>Category 2</th>
</tr>
</thead>
<tbody id="content">
</tbody>
<tbody>
</tbody>
</table>
&#13;
这是分类对象所属类别的部分,然后提取名字和姓氏。对于我的示例,它只是将其放入表格的列中,但是一旦提取了数据,您就可以对数据执行任何操作。
for (var i = 0; i < json.profiles.length; i++) {
var obj = json.profiles[i];
if (obj.category == 1) {
$("#content").append("<tr><td>" + obj.firstName + " " + obj.lastName + "</td><td></td></tr>");
}
else if (obj.category == 2) {
$("#content").append("<tr><td></td><td>" + obj.firstName + " " + obj.lastName + "</td></tr>");
}
}
基本上,你的代码中有循环,你只需要添加一个条件语句来检查对象属于哪个类别。