Javascript TypeScript:根据数字分割数组,并用空对象填充剩余的数组
所以我有一个小问题 我有一个对象数组,希望根据数字符号拆分它们。 然后,对象应该动态地存储在不同的数组中,但保留它们的索引,并在前后填充零。然后,不同的数组存储在单个数组中 例如:Javascript TypeScript:根据数字分割数组,并用空对象填充剩余的数组,javascript,arrays,typescript,algorithm,split,Javascript,Arrays,Typescript,Algorithm,Split,所以我有一个小问题 我有一个对象数组,希望根据数字符号拆分它们。 然后,对象应该动态地存储在不同的数组中,但保留它们的索引,并在前后填充零。然后,不同的数组存储在单个数组中 例如: const arr=[ {date:dayjs('12.02.2020').toDate(),profitValue:30}, {日期:dayjs('13.02.2020').toDate(),profitValue:-30}, {日期:dayjs('14.02.2020').toDate(),profitValue
const arr=[
{date:dayjs('12.02.2020').toDate(),profitValue:30},
{日期:dayjs('13.02.2020').toDate(),profitValue:-30},
{日期:dayjs('14.02.2020').toDate(),profitValue:-30},
{date:dayjs('15.02.2020').toDate(),profitValue:90},
];
电流输出:
[ { date: 2020-12-01T23:00:00.000Z, profitValue: 30 } ]
[ { date: Invalid Date, profitValue: -30 }, { date: Invalid Date, profitValue: -30 } ]
[ { date: Invalid Date, profitValue: 90 } ]
预计产量:
[ { date: 2020-12-01T23:00:00.000Z, profitValue: 30 }, null , null, null ]
[ null, { date: Invalid Date, profitValue: -30 }, { date: Invalid Date, profitValue: -30 }, null ]
[ null, null, null, { date: Invalid Date, profitValue: 90 } ]
这是到目前为止我的代码,因为我是一个初学者,它可能解决得很糟糕。
不幸的是,我不知道如何填充数组
从“dayjs”导入dayjs;
常数arr=[
{date:dayjs('12.02.2020').toDate(),profitValue:30},
{日期:dayjs('13.02.2020').toDate(),profitValue:-30},
{日期:dayjs('14.02.2020').toDate(),profitValue:-30},
{date:dayjs('15.02.2020').toDate(),profitValue:90},
];
函数splitArrayBySign(){
设sign=Math.sign(arr[0].profitValue);
让数据集=[];
设dataset=[];
用于(arr中的常数i){
if(sign==数学符号(arr[i].profitValue)){
push(arr[i]);
}
否则{
push(数据集);
数据集=[];
push(arr[i]);
}
符号=数学符号(arr[i].profitValue);
}
push(数据集);
返回数据集;
}
不确定dayjs是什么,所以我忽略了它,并将其用作常量:
var arr = [
{ date: '12.02.2020', profitValue: 30 },
{ date: '13.02.2020', profitValue: -30 },
{ date: '14.02.2020', profitValue: -30 },
{ date: '15.02.2020', profitValue: 90 },
];
考虑到这一点,您可以实现以下目标:
var result = arr.reduce((accum, record, index) => {
var group = accum[accum.length - 1];
//if we're the first group OR the sign is different
if(!group || Math.sign(group[group.length - 1].profitValue) !== Math.sign(record.profitValue)){
group = [];
accum.push(group);
//fill up the new grouping with nulls at the beginning
for(var x = 0; x < index; x++){
group.push(null);
}
}
group.push(record);
return accum;
}, []).map(group => {
//fill up the grouping with nulls at the end
for(var x = group.length; x < arr.length; x++){
group.push(null);
}
return group;
});
不确定dayjs是什么,所以我忽略了它,并将其用作常量:
var arr = [
{ date: '12.02.2020', profitValue: 30 },
{ date: '13.02.2020', profitValue: -30 },
{ date: '14.02.2020', profitValue: -30 },
{ date: '15.02.2020', profitValue: 90 },
];
考虑到这一点,您可以实现以下目标:
var result = arr.reduce((accum, record, index) => {
var group = accum[accum.length - 1];
//if we're the first group OR the sign is different
if(!group || Math.sign(group[group.length - 1].profitValue) !== Math.sign(record.profitValue)){
group = [];
accum.push(group);
//fill up the new grouping with nulls at the beginning
for(var x = 0; x < index; x++){
group.push(null);
}
}
group.push(record);
return accum;
}, []).map(group => {
//fill up the grouping with nulls at the end
for(var x = group.length; x < arr.length; x++){
group.push(null);
}
return group;
});