TypeScript。根据数字符号拆分数组,然后用空对象填充其余的数组。

问题描述 投票:0回答:1

所以,我有一个小问题。

我有一个对象的Array,想把它们按照数字符号分割开来,然后这些对象应该动态地存储在不同的Array中,但是要保持它们的索引,并且在前面和后面都要填上零。然后将不同的Array存储在一个Array中。

例子:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出:当前输出

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

javascript arrays typescript algorithm split
1个回答
2
投票

不知道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}]
    ]
]
© www.soinside.com 2019 - 2024. All rights reserved.