@gsdev99

Как сделать фильтрацию по датам?

Есть массив объектов с датами. И есть дата, начиная с которой с нужно отфильтровать массив. Например, за 6 месяцев, за 3 месяца, за 3 года и т.д.

Данные
const chartPoints = [
  {
    "date": "2017-01-31",
    "value": "-0.00014500"
  },
  {
    "date": "2017-10-31",
    "value": "0.00017500"
  },
  {
    "date": "2017-11-30",
    "value": "0.00001600"
  },
  {
    "date": "2017-12-31",
    "value": "-0.00015100"
  },
  {
    "date": "2018-01-31",
    "value": "-0.00025600"
  },
  {
    "date": "2018-02-28",
    "value": "-0.00004400"
  },
  {
    "date": "2018-12-30",
    "value": "-0.00760294"
  },
  {
    "date": "2019-01-13",
    "value": "-0.00161975"
  },
  {
    "date": "2019-01-14",
    "value": "0.00031716"
  },
  {
    "date": "2019-01-15",
    "value": "-0.00085519"
  },
  {
    "date": "2019-01-16",
    "value": "-0.00729198"
  },
  {
    "date": "2019-01-17",
    "value": "0.00209954"
  },
  {
    "date": "2019-01-18",
    "value": "-0.00397498"
  },
  {
    "date": "2019-01-19",
    "value": "0.00399084"
  },
  {
    "date": "2019-01-20",
    "value": "-0.00397498"
  },
  {
    "date": "2019-01-21",
    "value": "0.00050778"
  },
  {
    "date": "2019-01-22",
    "value": "0.00152410"
  },
  {
    "date": "2019-01-23",
    "value": "-0.00309089"
  },
  {
    "date": "2019-01-24",
    "value": "-0.00503519"
  },
  {
    "date": "2019-01-25",
    "value": "0.00330404"
  },
  {
    "date": "2019-01-26",
    "value": "-0.00329316"
  },
  {
    "date": "2019-01-27",
    "value": "0.00330404"
  },
  {
    "date": "2019-01-28",
    "value": "0.00125198"
  },
  {
    "date": "2019-01-29",
    "value": "0.00044354"
  },
  {
    "date": "2019-01-30",
    "value": "-0.00207310"
  },
  {
    "date": "2019-01-31",
    "value": "-0.00754435"
  },
  {
    "date": "2019-02-01",
    "value": "0.00194727"
  },
  {
    "date": "2019-02-02",
    "value": "-0.00194349"
  },
  {
    "date": "2019-02-03",
    "value": "0.00194727"
  },
  {
    "date": "2019-02-04",
    "value": "0.00170766"
  },
  {
    "date": "2019-02-05",
    "value": "-0.00189959"
  },
  {
    "date": "2019-02-06",
    "value": "0.00590438"
  },
  {
    "date": "2019-02-07",
    "value": "0.00096211"
  },
  {
    "date": "2019-02-08",
    "value": "-0.00197908"
  },
  {
    "date": "2019-02-09",
    "value": "0.00163139"
  },
  {
    "date": "2019-02-10",
    "value": "-0.00162874"
  },
  {
    "date": "2019-02-11",
    "value": "-0.00181984"
  },
  {
    "date": "2019-02-12",
    "value": "-0.00115103"
  },
  {
    "date": "2019-02-13",
    "value": "0.00206837"
  },
  {
    "date": "2019-02-14",
    "value": "0.01649665"
  },
  {
    "date": "2019-02-15",
    "value": "-0.00452178"
  },
  {
    "date": "2019-02-16",
    "value": "0.00454231"
  },
  {
    "date": "2019-02-17",
    "value": "-0.00452178"
  },
  {
    "date": "2019-02-18",
    "value": "0.00220610"
  },
  {
    "date": "2019-02-19",
    "value": "-0.00696247"
  },
  {
    "date": "2019-02-20",
    "value": "-0.00635345"
  },
  {
    "date": "2019-02-21",
    "value": "-0.00417836"
  },
  {
    "date": "2019-02-22",
    "value": "-0.00008116"
  },
  {
    "date": "2019-02-23",
    "value": "0.00008116"
  },
  {
    "date": "2019-02-24",
    "value": "-0.00008116"
  },
  {
    "date": "2019-02-25",
    "value": "0.00017887"
  },
  {
    "date": "2019-02-26",
    "value": "0.00491361"
  },
  {
    "date": "2019-02-27",
    "value": "0.00275353"
  },
  {
    "date": "2019-02-28",
    "value": "0.00003222"
  },
  {
    "date": "2019-03-01",
    "value": "-0.00069352"
  },
  {
    "date": "2019-03-02",
    "value": "0.00069399"
  },
  {
    "date": "2019-03-03",
    "value": "-0.00069352"
  },
  {
    "date": "2019-03-04",
    "value": "-0.00014531"
  },
  {
    "date": "2019-03-05",
    "value": "0.00138064"
  },
  {
    "date": "2019-03-06",
    "value": "0.00295905"
  },
  {
    "date": "2019-03-07",
    "value": "0.00162388"
  },
  {
    "date": "2019-03-08",
    "value": "-0.00205576"
  },
  {
    "date": "2019-09-04",
    "value": "-0.01084281"
  }
]

Начиная с указанной даты и до конца.
"startDate": "2019-01-16"
  • Вопрос задан
  • 2710 просмотров
Решения вопроса 2
0xD34F
@0xD34F Куратор тега JavaScript
function filterByMinMax(arr, [ min, max ], key = n => n) {
  const getVal = key instanceof Function ? key : n => n[key];
  return arr.filter(n => {
    const val = getVal(n);
    return (min == null || min <= val) && (max == null || val <= max);
  });
}


const d1 = filterByMinMax(chartPoints, [ new Date('2019-02-19') ], n => new Date(n.date));
const d2 = filterByMinMax(chartPoints, [ , '2019-01-23' ], n => n.date);
const d3 = filterByMinMax(chartPoints, [ '2019-02-12', '2019-02-26' ], 'date');
Ответ написан
@ettychel
Что первое в голову пришло:
Сделай .map, дополни новым свойством timestamp:
chartPoints.map(e => {
  return e[tamestamp] = Date.parse(e.date)
}

а потом сравнивай как хочешь по полю timestamp, а выводи chartPoints[n].date
Это наверное самый простой вариант
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы