简体   繁体   English

用javascript对象过滤json

[英]filter json with javascript object

That's my situation: I´ve got an HTML form, which is sperated in 4 sections, where the user can select some checkboxes and a drop down list. 这就是我的情况:我有一个HTML表单,该表单分为4个部分,用户可以在其中选择一些复选框和一个下拉列表。 When clicking on the button under this list, the selected elements will be thrown into an JS-Object. 单击此列表下的按钮时,所选元素将被扔到JS对象中。

Beneath that HTML form, I've got a result list, which is initially generated by a json file. 在该HTML表单下,我有一个结果列表,该列表最初由json文件生成。 When loading the page all objects in the json file are shown. 加载页面时,将显示json文件中的所有对象。 Now the user can select some filters in the html form to filter this list. 现在,用户可以在html表单中选择一些过滤器来过滤此列表。

Let me show you what I've got: 让我告诉你我所拥有的:

HTML form HTML表格

<form class="filterThisResults">
<ul class="filter-list">
    <button type="reset">delete filters</button>
    <div class="search-filter-section">
        <li>
            <h2>Bereich</h2>
        </li>
        <li>
            <input class="filterCheckbx" id="section" type="checkbox" name="section" value="Hochschule">
            <label for="check1">Hochschule</label>
        </li>
        <li>
            <input class="filterCheckbx" id="section" type="checkbox" name="section" value="Angewandte Ingenierwissenschaften">
            <label for="check2">Angewandte Ingenierwissenschaften</label>
        </li>
        <li>
            <input class="filterCheckbx" id="section" type="checkbox" name="section" value="Bauen & Gestalten">
            <label for="check3">Bauen & Gestalten</label>
        </li>
        <li>
            <input class="filterCheckbx" id="section" type="checkbox" name="section" value="BWL">
            <label for="check4">BWL</label>
        </li>
        <li>
            <input class="filterCheckbx" id="section" type="checkbox" name="section" value="Informatik">
            <label for="check5">Informatik</label>
        </li>
        <li>
            <input class="filterCheckbx" id="section" type="checkbox" name="section" value="Logistik">
            <label for="check6">Logistik</label>
        </li>
    </div>
    <div class="search-filter-group">
        <li>
            <h2>Gruppen</h2>
        </li>
        <li>
            <input class="filterCheckbx" id="group" type="checkbox" name="group" value="Professoren">
            <label for="check1">Professoren</label>
        </li>
        <li>
            <input class="filterCheckbx" id="group" type="checkbox" name="group" value="Studenten">
            <label for="check2">Studenten</label>
        </li>
        <li>
            <input class="filterCheckbx" id="group" type="checkbox" name="group" value="Angestellte">
            <label for="check3">Angestellte</label>
        </li>
    </div>
    <div class="search-filter-location">
        <li>
            <h2>Standort</h2>
        </li>
        <li>
            <input class="filterCheckbx" id="location" type="checkbox" name="location" value="Kaiserslautern">
            <label for="check1">Kaiserslautern</label>
        </li>
        <li>
            <input class="filterCheckbx" id="location" type="checkbox" name="location" value="Primasens">
            <label for="check2">Primasens</label>
        </li>
        <li>
            <input class="filterCheckbx" id="location" type="checkbox" name="location" value="Zweibrücken">
            <label for="check3">Zweibrücken</label>
        </li>
    </div>

    <div class="search-filter-topic">
        <li>
            <h2>Thema</h2>
        </li>           
        <li>    
            <select class="filterCheckbx" id="topic" name="topic" size="3">
                <option value="Lorem">Lorem</option>
                <option value="Ipsum">Ipsum</option>
                <option value="Dolor">Dolor</option>
            </select>   
        </li>
    </div>

    <li>
        <button class="submit-filter" type="submit">Ergebnisse anzeigen</button>
    <li>
</ul>   

JSON data JSON数据

{
"searchtest" : [
  {
    "section": "Hochschule",
    "group": "Professoren",
    "location": "Kaiserslautern",
    "date": "HS 2015/11",
    "description" : "Lorem ipsum dolor sit amet",
    "details" : "VZ",
    "deadline" : "27.12.2015",
    "topic" : "Lorem"
  },

  {
    "section": "Angewandte Ingenierwissenschaften",
    "group": "Studenten",
    "location": "Kaiserslautern",
    "date": "HS 2016/11",
    "description" : "Consetetur sadipscing elitr",
    "details" : "TZ",
    "deadline" : "01.01.2016",
    "topic" : "Ipsum"
  },

(...)

]}

Push selected elements into an object 将选定的元素推入对象

this.filterChkbx.on('click', function() {
    checkedBox.push({
        key: this.id,
        value: this.value
    });
    console.log('Selected filter: ', checkedBox);
});
this.submitFilter.on('click', function () {
    _this.filterList(checkedBox);
})

This works for me until here. 这对我一直有效,直到这里。 Clicking on this.filterChkbx pushes the id and the value of the checked item to the object checkedBox. 单击this.filterChkbx将选中项目的ID和值推送到对象checkbox。 This works - I can log the selected elements of the html formular. 这有效-我可以记录html公式的选定元素。 Now I want to filter my result list. 现在,我要过滤我的结果列表。 Here this.myObject references on the json array "searchtest". 这里this.myObject在json数组“ searchtest”上引用。 And thats where I am confused: 那就是我感到困惑的地方:

I iterate the json objects, and check if the key (of the filter object) matches el.section (which is the json object). 我迭代json对象,并检查(过滤器对象的)键是否匹配el.section(这是json对象)。 When true, I have to check if the value of this key, is the same, as in that json object. 当为true时,我必须检查此键的值是否与该json对象中的值相同。 And if that is true, show the item in the result. 如果是这样,则在结果中显示该项目。

filterList : function(filterArr){
    var _this = this;       
    var result = [];

    for(var i=0, i < this.myObject.length, i++) {           
        var el = this.myObject[i];

        if (filterArr[i].key === el.section){

        }
        if (filterArr[i].key === el.group){

        }
        if (filterArr[i].key === el.location){

        }
        if (filterArr[i].key === el.topic){

        }
     }
}

I´d like to achieve this with pure JS/jQuery. 我想使用纯JS / jQuery实现此目的。 I hope you guys got what I want to achieve. 我希望你们得到了我想要实现的目标。

Kind regards, David 亲切的问候,大卫

I made my own solution, I'd like to share with you: 我制定了自己的解决方案,并与您分享:

filterList : function(filterObj){


var _this = this;
    var result = [];

    for (var i = 0; i < _this.myObject.length; i++) {           
        var el = this.myObject[i];

        for (var j = 0; j < filterObj.length; j++){
            if (filterObj[j].filterEl == el.section){
                console.log('Filter section triggered on ', el.section);
            } else if (filterObj[j].filterEl == el.group){
                console.log('Filter group triggered on ', el.group);
            } else if (filterObj[j].filterEl == el.location){
                console.log('Filter location triggered on ', el.location);
            } else if (filterObj[j].filterEl == el.topic){
                console.log('Filter topic triggered on ', el.topic);
            };
            }   
        };      
     }

This does the comparsion, now just append the elements. 进行比较,现在仅添加元素。

If I understood your problem well, your filter function might look like this: 如果我很了解您的问题,则您的过滤器功能可能如下所示:

filterList : function(filterArr){
var _this = this;       
var result = [];

for(var i=0, i < this.myObject.length, i++) {           
    var el = this.myObject[i];

    if (filterArr[i].key === 'section'){

    }
    if (filterArr[i].key === 'group'){

    }
    if (filterArr[i].key === 'location'){

    }
    if (filterArr[i].key === 'topic'){

    }
  }
}

Since key seems to be initialized with your object's property's name, then it is a string which value is the litteral property's name (ie: topic, deadline and so on). 由于key似乎是用您对象的属性名称初始化的,因此它是一个字符串,其值是垃圾属性的名称(即主题,截止日期等)。

el.section will actually give you the object's section's value but not the name of the property. 实际上, el.section会为您提供对象的section的值,而不是属性的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM