Home > Blockchain >  Why javascript leaves one blank array but only in some cases
Why javascript leaves one blank array but only in some cases

Time:12-03

my code goes through quite a large number of arrays where everything is sorted so that it is in the right place. It flies 3 times through the input data to sort everything well, which causes empty arrays to appear. I try to delete them with the following code, but in some cases the code leaves me 1 empty array

The following code works correctly in most cases, but in some cases 1 array is left undeleted

chartTypeData.forEach((item: any, index: any) => {
        if (item.length === 0) {
            chartTypeData.splice(index, 1);
        }
});

Full code:

for (let i = 0; i < chartType.length; i  ) {
        const chartItem = chartsData?.product?.data[0].attributes.product_charts.data;
        for (let j = 0; j < chartCategory.length; j  ) {
            for (let k = 0; k < country.length; k  ) {
                chartTypeData.push(
                    chartItem.filter((item: any) => {
                        return (
                            item.attributes.type === chartType[i] &&
                            item.attributes.chartCategory === chartCategory[j] &&
                            item.attributes.country === country[k]
                        );
                    })
                );
            }

            chartTypeData[i].sort((a: any, b: any) => {
                return new Date(a.attributes.date).getTime() - new Date(b.attributes.date).getTime();
            });
        }
    }

    chartTypeData.forEach((item: any, index: any) => {
        if (item.length === 0) {
            chartTypeData.splice(index, 1);
        }
    });

CodePudding user response:

It looks like you are trying to delete empty arrays from the chartTypeData array. In your current code, you are iterating over the chartTypeData array and checking if the current item has a length of 0. If it does, you remove it from the array using the splice method.

However, the problem with this approach is that when you remove an item from the array, all the subsequent items are shifted to the left by one index. This means that some items may be skipped over and not checked for emptiness.

To fix this, you can use the filter method to create a new array containing only the non-empty items from chartTypeData, like this:

chartTypeData = chartTypeData.filter(item => item.length > 0);

This will create a new array containing only the non-empty items from chartTypeData, and then assign this new array to the chartTypeData variable. This should fix the issue and ensure that all empty arrays are removed from chartTypeData.

CodePudding user response:

It looks like you're trying to filter out empty arrays from chartTypeData using the code

chartTypeData.forEach((item: any, index: any) => {
   if (item.length === 0) {
     chartTypeData.splice(index, 1)
   }
})

This code will only remove empty arrays that are already present in chartTypeData. If you want to prevent empty arrays from being added to chartTypeData in the first place, you can add a check before adding the filtered arrays to chartTypeData:

for (let i = 0; i < chartType.length; i  ) {
    const chartItem = chartsData?.product?.data[0].attributes.product_charts.data;
    for (let j = 0; j < chartCategory.length; j  ) {
        for (let k = 0; k < country.length; k  ) {
            const filteredArray = chartItem.filter((item: any) => {
                return (
                    item.attributes.type === chartType[i] &&
                    item.attributes.chartCategory === chartCategory[j] &&
                    item.attributes.country === country[k]
                );
            });

            // Only add the filtered array to chartTypeData if it is not empty
            if (filteredArray.length > 0) {
                chartTypeData.push(filteredArray);
            }
        }

        chartTypeData[i].sort((a: any, b: any) => {
            return new Date(a.attributes.date).getTime() - new Date(b.attributes.date).getTime();
        });
    }
}

This way, you can avoid adding empty arrays to chartTypeData in the first place, and you won't need to remove them later.

  • Related