Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

106
Vistas
Javascript reduce method for object

Now I'm learning about reduce of Javascript. In below code, I don't understand acc[key] because we can access to properties of objects by using acc["key"] or acc.key . That's what I learned. but obviously the code below works correctly..why?

const arr = ['Cat', 'Dog', 'Bird', 'Cat']
const result = arr.reduce((acc, key) => {
    if (key in acc) {
        acc[key]++
    } else {
        acc[key] = 1
    }
    return acc
}, {})

console.log(result)
// { Cat: 2, Dog: 1, Bird: 1 }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So as @Alexandro mentioned that key is a string, indeed it is the value or values of the array arr which their type is string. Moreover We know that to access values of an object we follow two ways either square notation (e.g. arr[key]) or dot notation (e.g. arr.key). We perfer to use square notation in case there is a key made or contains space or other special chars.

about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda