filter json object by date range

1
  accounts =[
  {id: "1", name: "item 3", amount: "15" date:"10-10-2018"}
  {id: "2", name: "item 2", amount: "200" date:"9-11-2018"}
  {id: "3", name: "item 3", amount: "300" date:"16-12-2018"}
 ];

I have an array that stores json objects and they are ready in a table, I would like to know if it is possible to filter it by a range of dates using javascript, can you help me? What would be the correct way to do it?

    
asked by Michel Novellino 16.12.2018 в 16:30
source

2 answers

3

There are many ways to work with dates in JS.

Important clarification : the months in JS start from 0, January is the month 0 and December the month 11

The simplest thing to work with dates in JS is to work them in milliseconds, to do this you should break your dates using the object Date ; in your example it is not clear what format you are using, if dd-mm-yyyy or mm-dd-yyyy , I will assume that it is the first and that the first line of the array the 18 is a typing error, to decompose you could use a function like this:

function toMs(dateStr) {
  // desarmamos el string por los '-' los descartamos y lo transformamos en un array
  let parts = dateStr.split("-")
  // parts[2] es año
  // parts[1] el mes
  // parts[0] el día
  return new Date(parts[2], parts[1] - 1, parts[0]).getTime()
}

This function will return a value in milliseconds that you can use to compare with other values that you want, so to filter the array of accounts for example you could do:

let preDate = toMS("8-11-2018")
let postDate = toMS("10-11-2018")
let filteredAccounts = accounts.filter(function(account){
  return toMs(account.date) > preDate && toMs(account.date) < postDate
})

In any case if you can use external libraries it would be best to use Moment.js link

Bonus toMs feature in es6

const toMs = dateStr => {
  const [day, month, year] = dateStr.split("-")
  return new Date(year, month - 1, day).getTime()
}

Dates are a complicated topic in any programming language, there is much more to study than what can be developed in a response on this site, you can start here: link

    
answered by 16.12.2018 / 17:38
source
1

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"
}]
    
answered by 16.12.2018 в 17:44