How can I filter a json object with another object in Javascript (cannot use JQuery)
This is my JSON object
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"
}
]
The above is the filterable object, now below is the object for filtering
var filterArray = [{"id":"firstName","value":["Sam"]},{"id":"lastName","value":["Jones"]}]
I need to check the the fields of json object (jsondata) like firstName, lastName and match the values of both objects.
Suppose firstname field of jsondata will be compared with id of filterArray and their values will be matched. Alongside lastname will also be compared.
FInally I want to store the filtered object (after eliminating those which are not in filter in a variable). How can I realize this functionality? Please help.
I need to implement this using plain JS, I cannot use Jquery.