Na przykład mam następujący kod JSON, który zawiera wiele obiektów: [{"Średnia": 6.04, "Data": "2021-03-01", "najwyższy": 6.08, "...

1
Monochrome 15 marzec 2021, 18:49

2 odpowiedzi

Najlepsza odpowiedź

Można pokroić tablicę od końca i wybrać tylko poszukiwane właściwości.

const
    array = [{ average: 6.04, date: "2021-03-01", highest: 6.08, lowest: 5.99, order_count: 2345, volume: 3599480071 }, { average: 5.97, date: "2021-03-02", highest: 6.05, lowest: 5.85, order_count: 2335, volume: 5211678303 }, { average: 5.96, date: "2021-03-03", highest: 5.97, lowest: 5.75, order_count: 2268, volume: 3900129438 }, { average: 5.96, date: "2021-03-04", highest: 6.01, lowest: 5.76, order_count: 2142, volume: 4151340967 }, { average: 5.94, date: "2021-03-05", highest: 5.99, lowest: 5.72, order_count: 2279, volume: 4695138563 }, { average: 5.95, date: "2021-03-06", highest: 5.99, lowest: 5.76, order_count: 2598, volume: 6298670176 }, { average: 6.01, date: "2021-03-07", highest: 6.05, lowest: 5.91, order_count: 2832, volume: 4787097077 }, { average: 5.87, date: "2021-03-08", highest: 6.03, lowest: 5.74, order_count: 2462, volume: 5340747843 }, { average: 5.9, date: "2021-03-09", highest: 5.99, lowest: 5.67, order_count: 2176, volume: 5361380021 }, { average: 5.9, date: "2021-03-10", highest: 6, lowest: 5.63, order_count: 2353, volume: 5791738338 }, { average: 5.92, date: "2021-03-11", highest: 6.02, lowest: 5.71, order_count: 2332, volume: 4334212610 }, { average: 5.97, date: "2021-03-12", highest: 5.99, lowest: 5.91, order_count: 2097, volume: 3765328306 }, { average: 5.93, date: "2021-03-13", highest: 5.98, lowest: 5.8, order_count: 2482, volume: 6257745118 }, { average: 5.78, date: "2021-03-14", highest: 5.84, lowest: 5.61, order_count: 2803, volume: 7301956471 }]
    result = array
        .slice(-7)
        .map(({ average, date, volume }) => ({ average, date, volume }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
1
Nina Scholz 15 marzec 2021, 15:55

To wydaje się być dobrym kandydatem do reduce.

Możesz pętlić dane tylko raz i zmniejszyć go na podstawie filtrów i pożądanej struktury.

przykład:

const data = [{ average: 6.04, date: "2021-03-01", highest: 6.08, lowest: 5.99, order_count: 2345, volume: 3599480071 }, { average: 5.97, date: "2021-03-02", highest: 6.05, lowest: 5.85, order_count: 2335, volume: 5211678303 }, { average: 5.96, date: "2021-03-03", highest: 5.97, lowest: 5.75, order_count: 2268, volume: 3900129438 }, { average: 5.96, date: "2021-03-04", highest: 6.01, lowest: 5.76, order_count: 2142, volume: 4151340967 }, { average: 5.94, date: "2021-03-05", highest: 5.99, lowest: 5.72, order_count: 2279, volume: 4695138563 }, { average: 5.95, date: "2021-03-06", highest: 5.99, lowest: 5.76, order_count: 2598, volume: 6298670176 }, { average: 6.01, date: "2021-03-07", highest: 6.05, lowest: 5.91, order_count: 2832, volume: 4787097077 }, { average: 5.87, date: "2021-03-08", highest: 6.03, lowest: 5.74, order_count: 2462, volume: 5340747843 }, { average: 5.9, date: "2021-03-09", highest: 5.99, lowest: 5.67, order_count: 2176, volume: 5361380021 }, { average: 5.9, date: "2021-03-10", highest: 6, lowest: 5.63, order_count: 2353, volume: 5791738338 }, { average: 5.92, date: "2021-03-11", highest: 6.02, lowest: 5.71, order_count: 2332, volume: 4334212610 }, { average: 5.97, date: "2021-03-12", highest: 5.99, lowest: 5.91, order_count: 2097, volume: 3765328306 }, { average: 5.93, date: "2021-03-13", highest: 5.98, lowest: 5.8, order_count: 2482, volume: 6257745118 }, { average: 5.78, date: "2021-03-14", highest: 5.84, lowest: 5.61, order_count: 2803, volume: 7301956471 }];

const startOfLastWeek = new Date(
    new Date().setHours(0, 0, 0, 0) - 7 * 24 * 60 * 60 * 1000
).getTime();

function filterLastWeek(element) {
    return (new Date(element.date).getTime() >= startOfLastWeek);
}

function filterZeroValues(element) {
    return (
        element.average >= 0 &&
        element.volume >= 0 &&
        element.date !== ""
    );
}

const result = data.reduce((acc, curr) => {
    if (filterZeroValues(curr) && filterLastWeek(curr))
        acc.push({
            average: curr.average,
            date: curr.date,
            volume: curr.volume,
        });
    return acc;
}, []);

console.log(result);

Możesz także dostosować zakres daty, edytując startOfLastWeek, aby uniknąć hardcoded {x1}}.

0
Filip Seman 15 marzec 2021, 17:50