JSON - 如果对象的值等于

JSON - If Object's Value Equals

在我的 JSON 文件中,我有一个对象,其中包含多个对象的数组。如果对象的值等于某个数字,我想调用一个函数。如果相等,则只有该值的对象将被输入到选定的 div 中。解决此问题的最佳方法是什么?

示例:
我有一个 "profiles" 对象,想知道对象 "category" 是否等于数字“1”。如果是这样,将这些对象输入到第一个“.contentBox .viewed”。如果 "category" 对象等于数字“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" 中的内容是否可行,或者是否有更好的路线。

查看当前和完成Plunker

我为您做了一个非常基本的示例,它根据类别将名称加载到 table 的列中。

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>");
    }
}
table, tr, td, th{border: 1px solid black; border-collapse:collapse;}
<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>

这是整理对象属于哪个类别的部分,然后提取名字和姓氏。对于我的示例,它只是将其放入 table 的列中,但是一旦数据被提取,您可以对数据执行任何您想做的事情。

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>");
    }
}

基本上,你的代码中有循环,你只需要添加一个条件语句来检查对象属于哪个类别。