Home > Blockchain >  JavaScript array comparison and obtaining new array
JavaScript array comparison and obtaining new array

Time:11-05

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.

CodePudding user response:

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

  • Related