如何通过另一个对象过滤json对象-Javascript

时间:2019-01-02 03:36:31

标签: javascript json

如何用Javascript中的另一个对象过滤json对象(不能使用JQuery)

这是我的JSON对象

var jsondata = [
  {
    "firstName": "Sam",
    "lastName": "Jones",
    "age": "10"
  },
  {
    "firstName": "Sam1",
    "lastName": "Jones1",
    "age": "10"
  },
  {
    "firstName": "Sam2",
    "lastName": "Jones2",
    "age": "12"
  },
  {
    "firstName": "Sam3",
    "lastName": "Jones3",
    "age": "13"
  },
  {
    "firstName": "Sam4",
    "lastName": "Jones4",
    "age": "14"
  },
  {
    "firstName": "Sam5",
    "lastName": "Jones5",
    "age": "15"
  },
  {
    "firstName": "Sam",
    "lastName": "Jones11",
    "age": "16"
  },
  {
    "firstName": "Sam6",
    "lastName": "Jones6",
    "age": "17"
  },
  {
    "firstName": "Sam7",
    "lastName": "Jones7",
    "age": "18"
  },
  {
    "firstName": "Sam8",
    "lastName": "Jones8",
    "age": "19"
  },
  {
    "firstName": "Sam9",
    "lastName": "Jones9",
    "age": "20"
  },
  {
    "firstName": "Sam10",
    "lastName": "Jones10",
    "age": "21"
  },
  {
    "firstName": "Sam11",
    "lastName": "Jones11",
    "age": "22"
  },
  {
    "firstName": "Sam12",
    "lastName": "Jones12",
    "age": "23"
  }
]

上面是可过滤对象,现在下面是过滤对象

var filterArray = [{"id":"firstName","value":["Sam"]},{"id":"lastName","value":["Jones"]}]

我需要检查json对象(jsondata)的字段,例如firstName,lastName并匹配两个对象的值。

假设将jsondata的firstname字段与filterArray的ID进行比较,并且它们的值将匹配。除了姓氏,还将进行比较。

最后,我想存储过滤后的对象(在变量中消除那些不在过滤器中的对象之后)。如何实现此功能?请帮忙。

我需要使用普通的JS来实现,我不能使用Jquery。

2 个答案:

答案 0 :(得分:2)

您可以使用everyincludes来创建一个可以与filter()一起使用的布尔值。

例如:

var jsondata = [{"firstName": "Sam","lastName": "Jones","age": "10"},{"firstName": "Sam1","lastName": "Jones1","age": "10"},{"firstName": "Sam2","lastName": "Jones2","age": "12"},{"firstName": "Sam3","lastName": "Jones3","age": "13"},{"firstName": "Sam4","lastName": "Jones4","age": "14"},{"firstName": "Sam5","lastName": "Jones5","age": "15"},{"firstName": "Sam","lastName": "Jones11","age": "16"},{"firstName": "Sam6","lastName": "Jones6","age": "17"},{"firstName": "Sam7","lastName": "Jones7","age": "18"},{"firstName": "Sam8","lastName": "Jones8","age": "19"},{"firstName": "Sam9","lastName": "Jones9","age": "20"},{"firstName": "Sam10","lastName": "Jones10","age": "21"},{"firstName": "Sam11","lastName": "Jones11","age": "22"},{"firstName": "Sam12","lastName": "Jones12","age": "23"}]
  
var filterArray = [{"id":"firstName","value":["Sam"]},{"id":"lastName","value":["Jones"]}]

let filtered = jsondata.filter(item =>        // filter jsondata
       filterArray.every( f =>                // so every member of filter array
            f.value.includes(item[f.id])) )   // has a corresponding item[id] in value

console.log(filtered)

上面的过滤器说,对于every中的filterArray项,您希望value数组包括键为id的项。这样一来,jsondata中的项目就需要与filterArray中的所有条件匹配。如果将Sam1Jones1添加到数组,则将在过滤器中获得两项。

var jsondata = [{"firstName": "Sam","lastName": "Jones","age": "10"},{"firstName": "Sam1","lastName": "Jones1","age": "10"},{"firstName": "Sam2","lastName": "Jones2","age": "12"},{"firstName": "Sam3","lastName": "Jones3","age": "13"},{"firstName": "Sam4","lastName": "Jones4","age": "14"},{"firstName": "Sam5","lastName": "Jones5","age": "15"},{"firstName": "Sam","lastName": "Jones11","age": "16"},{"firstName": "Sam6","lastName": "Jones6","age": "17"},{"firstName": "Sam7","lastName": "Jones7","age": "18"},{"firstName": "Sam8","lastName": "Jones8","age": "19"},{"firstName": "Sam9","lastName": "Jones9","age": "20"},{"firstName": "Sam10","lastName": "Jones10","age": "21"},{"firstName": "Sam11","lastName": "Jones11","age": "22"},{"firstName": "Sam12","lastName": "Jones12","age": "23"}]
  
var filterArray = [{"id":"firstName","value":["Sam", "Sam1"]},{"id":"lastName","value":["Jones", "Jones1"]}]

let filtered = jsondata.filter(item =>        // filter jsondata
       filterArray.every( f =>                // so every member of filter array
            f.value.includes(item[f.id])) )   // has a corresponding item[id] in value

console.log(filtered)

答案 1 :(得分:0)

如果filterArray中的任何条件为false,则它将返回false,否则将返回true。 Array.prototype.filter()将创建一个新数组,其中包含该函数返回true的项。

const jsondata = jsondata=[{firstName:"Sam",lastName:"Jones",age:"10"},{firstName:"Sam1",lastName:"Jones1",age:"10"},{firstName:"Sam2",lastName:"Jones2",age:"12"},{firstName:"Sam3",lastName:"Jones3",age:"13"},{firstName:"Sam4",lastName:"Jones4",age:"14"},{firstName:"Sam5",lastName:"Jones5",age:"15"},{firstName:"Sam",lastName:"Jones11",age:"16"},{firstName:"Sam6",lastName:"Jones6",age:"17"},{firstName:"Sam7",lastName:"Jones7",age:"18"},{firstName:"Sam8",lastName:"Jones8",age:"19"},{firstName:"Sam9",lastName:"Jones9",age:"20"},{firstName:"Sam10",lastName:"Jones10",age:"21"},{firstName:"Sam11",lastName:"Jones11",age:"22"},{firstName:"Sam12",lastName:"Jones12",age:"23"}];

const filterArray = [{"id":"firstName","value":["Sam"]},{"id":"lastName","value":["Jones"]}];

var match = jsondata.filter(function(element){

    for(let i=0; i<filterArray.length; i++){
        if(!filterArray[i].value.includes(element[filterArray[i].id])){
            return false
        }
    }

    return true;

});

console.log(match);