I'm trying to figure out how to create an array that stores all the visits
values for each unique date without duplicating the date
arrays.
const MOCK = {
data: [
{date: "Aug.03", name: "Nihal Pandit", visits: 3 },
{date: "Aug.03", name: "Anthony Elias", visits: 3 },
{date: "Aug.04", name: "Alex P.", visits: 2 },
{date: "Aug.05", name: "Alex P.", visits: 3 },
{date: "Aug.05", name: "Anthony Elias", visits: 3 },
]
}
While looping over an array, I need a method to compare values between iterations. I think Array.reduce() might be helpful, but I'm not entirely sure how to implement it correctly just yet.
The desired output should resemble:
[["Aug.03", 3, 3], ["Aug.04", 2], ["Aug.05", 2, 3]]
Essentially, I want an array for each unique date containing all the visit
values from objects with that specific date.
let newArray = [];
let visitCountValues = MOCK?.data?.map((item, idx)=> {
let value = Object.values(item);
if(value[0] === value[0]){
newArray.push([value[0], value[1])
}
})