0

const hours = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24];

    const data = [
        {
            _id: '6239f41e0a9e6ba82ebc9774',
            title: 'X',
            capacity: 60,
            inParkings: [
                {
                    hour: 7,
                    count: 5,
                },
                {
                    hour: 19,
                    count: 4,
                },
                {
                    hour: 16,
                    count: 1,
                },
                {
                    hour: 8,
                    count: 11,
                },
                {
                    hour: 5,
                    count: 4,
                },
                {
                    hour: 9,
                    count: 3,
                },
                {
                    hour: 13,
                    count: 12,
                },
                {
                    hour: 14,
                    count: 6,
                },
                {
                    hour: 10,
                    count: 9,
                },
                {
                    hour: 23,
                    count: 1,
                },
                {
                    hour: 6,
                    count: 1,
                },
                {
                    hour: 12,
                    count: 8,
                },
                {
                    hour: 11,
                    count: 3,
                },
            ],
        },
        {
            _id: '62725362d9575e262f51ed84',
            title: 'Y',
            capacity: 75,
            inParkings: [
                {
                    hour: 13,
                    count: 6,
                },
                {
                    hour: 5,
                    count: 1,
                },
                {
                    hour: 14,
                    count: 2,
                },
                {
                    hour: 1,
                    count: 1,
                },
                {
                    hour: 6,
                    count: 1,
                },
            ],
        },
    ];

If the hour values ​​in the parkings data are missing from the data in the hours array, I want to add extra for the values ​​that are not

so for example, count 0 needs to be entered for hours that are not as follows

the code i wrote

data.map((m) => {
            return {
                name: m.title,
                data: m.inParkings.slice().sort((a, b) => {
                    return a.hour - b.hour;
                }),
            };
        })

I want to get a similar output

[{
  data: [{
  count: 4,
  hour: 5
}, {
  count: 1,
  hour: 6
}, {
  count: 5,
  hour: 7
}, {
  count: 11,
  hour: 8
}, {
  count: 3,
  hour: 9
}, {
  count: 9,
  hour: 10
}, {
  count: 3,
  hour: 11
}, {
  count: 8,
  hour: 12
}, {
  count: 12,
  hour: 13
}, {
  count: 6,
  hour: 14
}, {
  count: 1,
  hour: 16
}, {
  count: 4,
  hour: 19
}, {
  count: 1,
  hour: 23
}],
  name: "X"
}, {
  data: [{
  count: 1,
  hour: 1
}, {
  count: 1,
  hour: 5
}, {
  count: 1,
  hour: 6
}, {
  count: 6,
  hour: 13
}, {
  count: 2,
  hour: 14
}, {
  count: 0,
  hour: 18,
}],
  name: "Y"
}]

For example, since there is no 18 value in the hours array in parkings, I need to enter the value of the relevant hour as 0 in the array I just created.

I may have explained it a bit confused, but briefly, if there is no clock in the hours array in the parking data, I need to add the relevant time to the data and return count 0.

2
  • There are 2 arrays. How to decide which inner array the missing hour goes to? Commented Nov 4, 2022 at 14:13
  • I'm stuck on checking and adding in both arrays Commented Nov 4, 2022 at 14:18

1 Answer 1

1

You could collect the counts in an object and map hours with count or zero.

const
    hours = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
    data = [{ _id: '6239f41e0a9e6ba82ebc9774', title: 'X', capacity: 60, inParkings: [{ hour: 7, count: 5 }, { hour: 19, count: 4 }, { hour: 16, count: 1 }, { hour: 8, count: 11 }, { hour: 5, count: 4 }, { hour: 9, count: 3 }, { hour: 13, count: 12 }, { hour: 14, count: 6 }, { hour: 10, count: 9 }, { hour: 23, count: 1 }, { hour: 6, count: 1 }, { hour: 12, count: 8 }, { hour: 11, count: 3 }], }, { _id: '62725362d9575e262f51ed84', title: 'Y', capacity: 75, inParkings: [{ hour: 13, count: 6 }, { hour: 5, count: 1 }, { hour: 14, count: 2 }, { hour: 1, count: 1 }, { hour: 6, count: 1 }] }],
    getData = parkings => {
        const
            counts = Object
                .fromEntries(parkings.map(({ hour, count }) => [hour, count]));

        return hours.map(hour => ({ hour, count: counts[hour] || 0 }));
    },
    result = data.map(({ title: name, inParkings }) => ({ data: getData(inParkings), name }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

5 Comments

thanks how do we get the data array to be an array that contains only count, so it's like this [{ data: [4,1,5,11], name: "X" }, { data: [1,1,0,0], name: "Y" }]
where do you get the hours from?
Each count in data corresponds to the hour, but I don't need the hour values ​​when printing the data. This is used for a chart structure, the hours are kept on a separate attribute
please ask a new question with the different data scheme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.