简体   繁体   English

以对象为参数的Javascript JSON文件过滤器

[英]Javascript JSON file filter with object as a parameter

I'm a newbie in Javascript and I'm trying to understand the destructuring approach also with object literals.我是 Javascript 的新手,我正在尝试使用对象字面量来理解解构方法。 So what I'm trying to do is to create a function which has two types of arguments : 1. It's a JSON data file which I want to iterate.所以我想要做的是创建一个函数,它有两种类型的参数: 1. 这是一个我想要迭代的 JSON 数据文件。 2. An object literal with a random value assigned. 2. 分配了随机值的对象字面量。 So I'm trying to iterate with this object value passed as a parameter and filter with the data from the JSON file with an if statement inside the array of objects iterator.因此,我尝试使用作为参数传递的这个对象值进行迭代,并使用对象迭代器数组中的if 语句过滤来自 JSON 文件的数据。 And add to arr all the object which match.并将所有匹配的对象添加到arr中。 Thanks everyone in advance.提前谢谢大家。

Array of objects:对象数组:

[
  { "id": 44, "hours": 100,"finished": false },
  { "id": 22, "hours": 80,"finished": false },
  { "id": 65, "hours": 34,"finished": false },
  { "id": 1098, "hours": 21,"finished": true  },
  { "id": 2, "hours": 67,"finished": false },
  { "id": 765, "hours": 32,"finished": false },
  { "id": 223, "hours": 555,"finished": false },
  { "id": 986, "hours": 2,"finished": false }
]

main.js主文件

const data = require('./example.json')

function dataFilter (items, {id: _id, hours: _hours, finished: _finished}) {

for (let i = 0; i < items.length; i++) {
    let arr = [];
    if (items[i].id === _id) {
        arr.push(items[i])
    }
    else if (items[i].hours >= _hours){
        arr.push(items[i])
    }
    else if (items[i].finished === finished){
        arr.push(items[i])
    }
    return arr;
  }
}

console.log(dataFilter(data,{ id: 65 }));
console.log(dataFilter(data,{ hours: 30 }));

You don't need destructuring, what you need is array filter.您不需要解构,您需要的是数组过滤器。

You also forgot to set a default {} so you can access undefined keys:您还忘记设置默认{}以便您可以访问undefined键:

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/filter https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/filter

 const data = [ { "id": 44, "hours": 100,"finished": false }, { "id": 22, "hours": 80,"finished": false }, { "id": 65, "hours": 34,"finished": false }, { "id": 1098, "hours": 21,"finished": true }, { "id": 2, "hours": 67,"finished": false }, { "id": 765, "hours": 32,"finished": false }, { "id": 223, "hours": 555,"finished": false }, { "id": 986, "hours": 2,"finished": false }, { "id": 1986, "hours": 30,"finished": false }, ]; function dataFilter (items, {id: _id, hours: _hours, finished: _finished} = {}) { return items.filter((item) => item.id === _id || item.hours >= _hours || item.finished === _finished); } document.getElementById('results').innerHTML = ` <pre> ID: 65 ${JSON.stringify(dataFilter(data,{ id: 65 }), null, 2)} HOURS: 30 ${JSON.stringify(dataFilter(data,{ hours: 30 }), null, 2)} </pre> `
 <div id="results"></div>

Approach with multiple filters使用多个过滤器的方法

It is also possible to use more than one filter at once:也可以一次使用多个过滤器:

 const data = [ { "id": 44, "hours": 100,"finished": false }, { "id": 22, "hours": 80,"finished": false }, { "id": 65, "hours": 34,"finished": false }, { "id": 1098, "hours": 21,"finished": true }, { "id": 2, "hours": 67,"finished": false }, { "id": 765, "hours": 32,"finished": false }, { "id": 223, "hours": 555,"finished": false }, { "id": 986, "hours": 2,"finished": false }, { "id": 1986, "hours": 30,"finished": false }, ]; function dataFilter (items, filters = {}) { // this will create a list of function on the fly for every `filters` you pass. const fnList = Object.keys(filters) .map((key) => (list) => list.filter((item) => item[key] === filters[key])); let res = [...items]; while (cursor = fnList.shift()) { res = cursor(res); } return res; } document.getElementById('results').innerHTML = ` <pre> ID: 44, HOURS: 100 ${JSON.stringify(dataFilter(data,{ id: 44, hours: 100 }), null, 2)} ID: 2, HOURS: 67 ${JSON.stringify(dataFilter(data,{ id: 2 }), null, 2)} </pre> `
 <div id="results"></div>

If you want to specify the operators used for the comparaison, use a fonction as explained here: Are Variable Operators Possible?如果要指定用于比较的运算符,请使用此处解释的函数: 变量运算符是否可能?

Looks like you want to be able to filter the data by any combination of the three items in that object.看起来您希望能够通过该对象中三个项目的任意组合来过滤数据。

function filterFactory({id, hours, finished}) {
  return function filter(item) {
    let isGoodValue = false;
    if (id !== undefined && item.id === id) isGoodValue = true;

    // for the second and third checks we'll short-circuit if it already
    // passed an earlier check
    if (!isGoodValue && hours !== undefined && item.hours >= hours) isGoodValue = true;
    if (!isGoodValue && finished !== undefined && item.finished === finished) isGoodValue = true;
    return isGoodValue;
  };
}

data.filter(filterFactory({id: 2})); 

Note that we're using the native filter method on arrays.请注意,我们在数组上使用本机过滤器方法 filterFactory is a factory that makes callbacks to pass to filter based on one or more of the three factors you're filtering on. filterFactory是一种工厂,它根据您过滤的三个因素中的一个或多个因素使回调传递给过滤器。

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

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