使用Javascript forEach和indexOf在JSON中搜索多个过滤器 [英] Search for multiple filters in JSON using Javascript forEach and indexOf

查看:59
本文介绍了使用Javascript forEach和indexOf在JSON中搜索多个过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JSON格式的数据,我需要在其中执行搜索。有不同的标签可用,当我点击它们时,它会搜索JSON并返回包含这些标签的项目。为此,我使用的是js函数。它第一次正常工作,但当我在函数中推送第二个过滤器时,它返回错误的数据。

I have data in JSON format in which I need to perform search. There are different tags available and when I click on them it searches in JSON and returns the items which has those tags. For this I am using a js function. It works correctly for the first time but When I push second filter in the function it returns wrong data.

可用的过滤器是:

绑定


  • 平装书

  • 精装
  • li>
  • 有声读物

  • 盒装套装

  • Paperback
  • Hardcover
  • Audiobook
  • Boxed Set

类别


  • Classic Rock

  • Pop

  • Pop Rock

  • Electro Pop

  • Soft Rock

  • Rock

  • Classic Rock
  • Pop
  • Pop Rock
  • Electro Pop
  • Soft Rock
  • Rock

语言


  • 德语

  • 英语

  • 法语

作者


  • 男性

  • 女性

  • 男/女

这是我正在使用的JSON和代码:

Here is the JSON and code I am using:

var m = {
      "Books": [{
          "title": "Book 1",
          "binding": "paperback",
          "category": "pop",
          "language": "english",
          "author": "male"
        },
        {
          "title": "Book 2",
          "binding": "hardcover",
          "category": "pop rock,electro pop",
          "language": "french",
          "author": "female"
        },
        {
          "title": "Book 3",
          "binding": "audiobook",
          "category": "soft rock",
          "language": "german",
          "author": "male,female"
        },
        {
          "title": "Book 4",
          "binding": "boxed set",
          "category": "rock,classic rock",
          "language": "english",
          "author": "female,male"
        },
        {
          "title": "Book 5",
          "binding": "paperback",
          "category": "electro pop,rock,classic rock",
          "language": "french",
          "author": "male/female"
        },
        {
          "title": "Book 6",
          "binding": "paperback",
          "category": "rock",
          "language": "french",
          "author": "male"
        }
      ]
    }


    // a function which accepts key which is one of binding,category,language,author.
    // the array will be filtered on this key
    function getFilteredElement(key, value) {
        var bookFilter = [];
        m.Books.forEach(function(item){
           var getFilterField = item[key];
           // since the value is a string, so splitting it and creating an array
           var keyArray = item[key].split(',');
           // now checking if the passed value has a presence in the  above array
           if (keyArray.indexOf(value) !== -1) {
               // if present pushed the book name
               bookFilter.push(item.title);
           }
       });
        // returning the array of books
        return bookFilter;
    }

    console.log(getFilteredElement('category', 'rock'))

例如当我按 category = rock 时,它会返回第4册,第5册和第6册但是如果我按 category = rock language = french ,返回的结果应该只是预订5 但它没有返回正确的结果。

For e.g. When I push category = rock it returns Book 4, Book 5 and Book 6 but If I push category = rock and language = french, the returned result should only be Book 5 but it doesn't return correct results.

任何人都可以提供帮助。

Could anyone please help.

推荐答案

您可以使用过滤器的对象,并将键作为要搜索的键,值为字符串的精确值。

You could use an object for filters and take the keys the key to search for and the value as excact value for the string.

分割并检查带逗号的属性。

Properties with commas are splitted and checked.

function getBooks(filters) {
    return array.Books.filter(function (o) {
        return Object.keys(filters).every(function (k) {
            return o[k].split(',').some(function (v) {
                return v === filters[k];
            });
        });
    });
    //.map(function (o) {
    //    return o.title;
    //});
}

var array = { Books: [{ title: "Book 1", binding: "paperback", category: "pop", language: "english", author: "male" }, { title: "Book 2", binding: "hardcover", category: "pop rock,electro pop", language: "french", author: "female" }, { title: "Book 3", binding: "audiobook", category: "soft rock", language: "german", author: "male,female" }, { title: "Book 4", binding: "boxed set", category: "rock,classic rock", language: "english", author: "female,male" }, { title: "Book 5", binding: "paperback", category: "electro pop,rock,classic rock", language: "french", author: "male/female" }, { title: "Book 6", binding: "paperback", category: "rock", language: "french", author: "male" }] };

console.log(getBooks({ category: 'rock' }));
console.log(getBooks({ category: 'rock', language: 'french' }));

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于使用Javascript forEach和indexOf在JSON中搜索多个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆