简体   繁体   English

长度未知的JS过滤器数组或arrays?

[英]JS filter array or arrays with unknown length?

I have some data I'm trying to transform by filtering by date, however the data can vary in length ie like this;我有一些我试图通过按日期过滤来转换的数据,但是数据的长度可能会有所不同,例如这样;

longData = [
     // These objects could be unlimited but the name key will always be unique
    {name: Opt 1,
     Data:
         // The first value will always be a date string and the second is the amount, the length of this array is always unknown
         ['2021-01-02', 30],
         ['2021-01-03, 20],
         ['2021-02-05', 15]
     },
     {name: Opt 2,
     Data:
         ['2021-01-02', 30],
         ['2021-02-08, 20],
         ['2021-04-02, 15]
     },
     {name: Opt 3,
     Data:
         ['2021-03-02', 30],
         ['2021-03-04, 20]
     }

What I want to do is essentially create a new object that has the same shape and the original but only includes the data between my dates.我想要做的基本上是创建一个新的 object,它具有与原始相同的形状,但只包含我日期之间的数据。

Ie if I have a start date of '2021-01-01' and end of date '2021-01-07'即,如果我的开始日期为“2021-01-01”,结束日期为“2021-01-07”

I should get an object like the following..我应该得到一个 object 如下所示..

shortData = [
    {name: Opt 1,
     Data:
         ['2021-01-02', 30],
         ['2021-01-03, 20]
     },
     {name: Opt 2,
     Data:
         ['2021-01-02', 30]
     },
     {name: Opt 3,
     Data:
         []
     }

I'm trying to break it down by week, these numbers are rendered in a chart and when dealing with large data sets it can break the chart essentially, so I'm trying to page it by week.我试图按周对其进行分解,这些数字呈现在图表中,并且在处理大型数据集时,它可能会破坏图表,因此我试图按周对其进行分页。

Any thoughts on how to accomplish it would be rather helpful.关于如何完成它的任何想法都会很有帮助。

You can create a function filterByDate which takes your start date and end date strings and converts them into date objects.您可以创建一个 function filterByDate ,它将您的开始日期和结束日期字符串转换为日期对象。 Then, for each object in longData , you can map it to a transformed object containing a new Data property, which is the filtered version of your Data array.然后,对于 longData 中的每个longData ,您可以将map转换为包含新Data属性的转换后的 object ,这是您的Data数组的过滤版本。

 const longData = [{ name: "Opt 1", Data: [ ['2021-01-02', 30], ['2021-01-03', 20], ['2021-02-05', 15] ] }, { name: "Opt 2", Data: [ ['2021-01-02', 30], ['2021-02-0', 8, 20], ['2021-04-02', 15] ] }, { name: "Opt 3", Data: [ ['2021-03-02', 30], ['2021-03-04', 20] ] } ]; const isBetween = (dx, d1, d2) => d1 < dx && dx < d2; const filterByDate = (data, start, end) => { const startDate = new Date(start); const endDate = new Date(end); return data.map(obj => ({...obj, Data: obj.Data.filter( ([date]) => isBetween(new Date(date), startDate, endDate) )})); } const res = filterByDate(longData, '2021-01-01', '2021-01-07'); console.log(res);

Given the following array of objects:给定以下对象数组:

const data = [
  {
    name: "Opt 1",
    Data: [
      ["2021-01-02", 30],
      ["2021-01-03", 20],
      ["2021-02-05", 15],
    ],
  },
  {
    name: "Opt 2",
    Data: [
      ["2021-01-02", 30],
      ["2021-02-08", 20],
      ["2021-04-02", 15],
    ],
  },
  {
    name: "Opt 3",
    Data: [
      ["2021-03-02", 30],
      ["2021-03-04", 20],
    ],
  },
];

And you simply want to return the same array so that it filters Data property (which is an array of unknown length) by a given date (ie "2021-01-02" ), I would use Array.prototype.reduce , spread syntax , and Array.prototype.filter to create the new data but with the Data property filtered so that it matches the given date (or in your case, falls within a start & end date):而且您只想返回相同的数组,以便它按给定日期(即"2021-01-02" )过滤Data属性(这是一个未知长度的数组),我会使用Array.prototype.reduce传播语法, 和Array.prototype.filter来创建新数据,但对Data属性进行过滤,使其与给定日期匹配(或者在您的情况下,属于开始和结束日期):

let newData = data.reduce(
  (acc, curr) => [
    ...acc,
    { ...curr, Data: curr.Data.filter((d) => d[0] === "2021-01-02") },
  ],
  []
);

// [
//   { name: "Opt 1", Data: [["2021-01-02", 30]] },
//   { name: "Opt 2", Data: [["2021-01-02", 30]] },
//   { name: "Opt 3", Data: [] },
// ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM