首页 > 解决方案 > TypeScript:根据数字符号拆分数组并用空对象填充其余部分

问题描述

所以我有一个小问题,

我有一个对象数组,想根据数字符号拆分它们。然后对象应该动态存储在不同的数组中,但保持它们的索引并在前后用零填充。然后将不同的数组存储在一个数组中。

例子:

const arr = [
    { date: dayjs('12.02.2020').toDate(), profitValue: 30 },
    { date: dayjs('13.02.2020').toDate(), profitValue: -30 },
    { date: 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 } ]

到目前为止,这是我的代码,因为我是初学者,所以可能解决得不好。不幸的是,我不知道如何填充数组。

import dayjs from "dayjs";
const arr = [
    { date: dayjs('12.02.2020').toDate(), profitValue: 30 },
    { date: dayjs('13.02.2020').toDate(), profitValue: -30 },
    { date: dayjs('14.02.2020').toDate(), profitValue: -30 },
    { date: dayjs('15.02.2020').toDate(), profitValue: 90 },
];
function splitArrayBySign() {

    let sign = Math.sign(arr[0].profitValue);
    let datasets = [];
    let dataset = [];
    for (const i in arr) {
        if (sign === Math.sign(arr[i].profitValue)) {
            dataset.push(arr[i]);
        }
        else {
            datasets.push(dataset);
            dataset = [];
            dataset.push(arr[i]);
        }
        sign = Math.sign(arr[i].profitValue);
    }
    datasets.push(dataset);
    return datasets;
}

标签: javascriptarraystypescriptalgorithmsplit

解决方案


不确定 dayjs 是什么,所以我忽略了它,而是使用它作为 const:

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;
});

产生这个输出:

[
    [
        {"date":"12.02.2020","profitValue":30},
        null,
        null,
        null
    ],
    [
        null,
        {"date":"13.02.2020","profitValue":-30}, 
        {"date":"14.02.2020","profitValue":-30},
        null
    ],
    [
        null,
        null,
        null,
        {"date":"15.02.2020","profitValue":90}]
    ]
]

推荐阅读