JS过滤数组或长度未知的数组?
JS filter array or arrays with unknown length?
我有一些数据正在尝试通过按日期过滤来转换,但是数据的长度可能会有所不同,例如:
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]
}
我想做的基本上是创建一个与原始对象具有相同形状但仅包含我的日期之间的数据的新对象。
即如果我的开始日期为“2021-01-01”,结束日期为“2021-01-07”
我应该得到一个像下面这样的对象..
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:
[]
}
我试图按周细分,这些数字呈现在图表中,当处理大型数据集时,它基本上会破坏图表,所以我试图按周对其进行分页。
任何关于如何完成它的想法都会很有帮助。
您可以创建一个函数 filterByDate
,它将您的开始日期和结束日期字符串转换为日期对象。然后,对于 longData
中的每个对象,您可以 map it to a transformed object containing a new Data
property, which is the filtered 版本的 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);
给定以下对象数组:
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],
],
},
];
你只是想 return 相同的数组,以便它在给定日期(即 "2021-01-02"
),我会使用 Array.prototype.reduce, spread syntax, and 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: [] },
// ];
我有一些数据正在尝试通过按日期过滤来转换,但是数据的长度可能会有所不同,例如:
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]
}
我想做的基本上是创建一个与原始对象具有相同形状但仅包含我的日期之间的数据的新对象。
即如果我的开始日期为“2021-01-01”,结束日期为“2021-01-07”
我应该得到一个像下面这样的对象..
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:
[]
}
我试图按周细分,这些数字呈现在图表中,当处理大型数据集时,它基本上会破坏图表,所以我试图按周对其进行分页。
任何关于如何完成它的想法都会很有帮助。
您可以创建一个函数 filterByDate
,它将您的开始日期和结束日期字符串转换为日期对象。然后,对于 longData
中的每个对象,您可以 map it to a transformed object containing a new Data
property, which is the filtered 版本的 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);
给定以下对象数组:
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],
],
},
];
你只是想 return 相同的数组,以便它在给定日期(即 "2021-01-02"
),我会使用 Array.prototype.reduce, spread syntax, and 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: [] },
// ];