• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

104
Vistas
Looping over an array of objects and creating an array for each item with the same value at a certain index

I need to create an array that contains all the visits values if the date is the same without creating duplicate 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 },
        ]
     }

But im not sure of a method that lets you compare the values from one iteration to another when looping over an array. I think Array.reduce() might work, but I dont understand how to properly use reduce at this point.

I am looking for a result that looks like:

[["Aug.03", 3, 3], ["Aug.04",2],["Aug.05", 2, 3]

So I need an array for each date (the array should contain that date) and all the visit values from every object that contains that 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])
      }
        
      }) 
over 2 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

after you update you question I understood better you can do something like this

const visits = [
      {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 },
    ]
    
const result = Object.values(visits.reduce((res, {date, visits}) =>{
   const existing = res[date] || [date]
   res[date] = [...existing, visits]
   return res
}, {}))

console.log(result)

over 2 years ago · Juan Pablo Isaza Denunciar

0

You can use reduce to group the array on one objectwithdateas property, andObject.valuesto get themapped` 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 },
        ]
     }
     
const dateGroups = Object.values(MOCK.data.reduce((acc, { date, visits }) => ({
  ...acc,
  [date]: [...(acc[date] || [date]), visits]
}), {}))

console.log(dateGroups)

over 2 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda