简体   繁体   English

如何使用网页中的多个不同参数对 JSON 进行排序?

[英]How to sort JSON with multiple different parameters from a webpage?

For a school I am creating a program that uses JSON and JavaScript to pull back data onto a webpage.对于一所学校,我正在创建一个使用 JSON 和 JavaScript 将数据拉回网页的程序。 I understand how to sort through the array and pull back values of they match.我了解如何对数组进行排序并撤回它们匹配的值。 Lets says I have this JSON.让我们说我有这个 JSON。

{"name":"Michael","age":30,"bday":"05/12/1982"}
{"name":"Andy", "age":30,"bday":"02/21/1992"}
{"name":"Justin", "age":19,"bday":"12/07/1972"}

How would I be able to have a name input, age select and a birthday input that I could sort by any combination of those being used.我如何能够输入姓名,年龄选择和生日输入,我可以按使用的任何组合进行排序。

Say everyone that is 30 and born on 02/21/1992, or everyone that is 30?说 30 岁和 1992 年 2 月 21 日出生的每个人,还是 30 岁的每个人? I want to be able to sort by every possible combination of data entry and I do not want to just use a bunch of if() statements.我希望能够按数据条目的每种可能组合进行排序,并且我不想只使用一堆 if() 语句。

The algorithm looks for every property in search and if all comparisons are true, then the element is added to the result set.该算法在search查找每个属性,如果所有比较都为真,则将该元素添加到结果集中。

 function filter(array, search) { return array.filter(function (a) { return Object.keys(search).every(function (k) { return ( a[k] === search[k] || typeof search[k] === 'object' && +search[k].min <= a[k] && a[k] <= +search[k].max || typeof search[k] === 'function' && search[k](a[k]) ); }); }); } var data = [{ name: "Michael", age: 30, bday: "05/12/1982" }, { name: "Andy", age: 30, bday: "02/21/1992" }, { name: "Justin", age: 19, bday: "12/07/1972" }]; // serach for name with n document.write('<pre>' + JSON.stringify(filter(data, { name: function (s) { return s.match(/n/i); } }), 0, 4) + '</pre>'); // search for age = 30 document.write('<pre>' + JSON.stringify(filter(data, { age: 30 }), 0, 4) + '</pre>'); // search for age = 30 and bday = 02/21/1992 document.write('<pre>' + JSON.stringify(filter(data, { age: 30, bday: '02/21/1992' }), 0, 4) + '</pre>'); // search for age between 18 and 20 document.write('<pre>' + JSON.stringify(filter(data, { age: { min: 18, max: 20} }), 0, 4) + '</pre>');

you can have input for filtring information in ur HTML file您可以在您的 HTML 文件中输入过滤信息

<input type="search" id="search" placeholder="search">

and JS file like this和这样的JS文件

$("#search").keyup(function () {
var searchField = $("#search").val();
// RegExp for case sensitive 
var myReg = new RegExp(searchField,"i");
$.getJSON('data.json',function (data) {
    var output ="<ul class='searchresults'>";
    $.each(data,function(key,val){
        if((val.name.search(myReg) != -1 ) || (val.info.search(myReg) != -1)){

            output +='<li>';
            output += "<h2>"+val.name+"</h2>";
            output += "<p>"+val.age+"</p>";
            output+='</li>';
        }
    });
    output +='</ul>';
    $("#update").html(output)
});

}); });

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

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