You can use the filter
method to perform this process through
of the key you choose.
First we have declared our JSON associated with a var named accounts
let accounts =[
{"id": "1", "name": "item 1", "amount": "150", "date":"10-18-2018"},
{"id": "2", "name": "item 2", "amount": "200", "date":"11-18-2018"},
{"id": "3", "name": "item 3", "amount": "600", "date":"11-18-2018"},
{"id": "4", "name": "item 4", "amount": "700", "date":"12-18-2018"},
{"id": "5", "name": "item 5", "amount": "800", "date":"13-18-2018"}
];
EXAMPLE 1
Here we need to filter by date, indicating that it only shows us those
that are greater than "10-18-2018"
As you can see I do n.date
is n the var I use to access the key
date
and make my comparison
let busca = accounts.filter(n => n.date == "10-18-2018")
Finally we print the same variable
console.log(busca)
FINAL RESULT EXAMPLE 1
[[object Object] {
amount: "150",
date: "10-18-2018",
id: "1",
name: "item 1"
}]
EXAMPLE 2
If now I need to apply a filter to extract a group of dates, I can use the logical operator AND
represented by &&
in this way
let accounts =[
{"id": "1", "name": "item 1", "amount": "150", "date":"10-18-2018"},
{"id": "2", "name": "item 2", "amount": "200", "date":"11-18-2018"},
{"id": "3", "name": "item 3", "amount": "600", "date":"11-18-2018"},
{"id": "4", "name": "item 4", "amount": "700", "date":"12-18-2018"},
{"id": "5", "name": "item 5", "amount": "800", "date":"13-18-2018"}
];
let busca = accounts.filter(n => n.date > "10-18-2018" && n.date < "12-18-2018")
console.log(busca)
FINAL RESULT OF EXAMPLE 2
[[object Object] {
amount: "200",
date: "11-18-2018",
id: "2",
name: "item 2"
}, [object Object] {
amount: "600",
date: "11-18-2018",
id: "3",
name: "item 3"
}]