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

55
Vistas
How to map through an object with array of object

I have an object which has an array of objects and 2 other key-value pairs. I get stuck on how to display the object values. I got an error showing 'TypeError: obj.map is not a function.

This is my code

import React from 'react'

function MapObj() {

    const obj = {
        items: [
          {
          id: 1,
          title: 'Pizza'
        },
        {
         id: 2,
         title: 'Hot-Dog'
       }
       ],
        total: 200,
        isEmpty: false
      };

  const mappedObj = obj.map(item => {
      return (
       <>
         <div key={item.items.id}>
          <h2>{item.items.title}</h2>
         </div>
        <p>{item.total}</p>
        <p>{item.isEmpty}</p>
       </>
      )
  })

  return (
    <div style={{textAlign: 'center', color: 'maroon'}}>
        {mappedObj}
    </div>
  )
}

export default MapObj
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The function map does not exist on an object but only on an array.

Try to use map on object.items instead on the object directly. Also you can access the property using item.id for example directly.

Like this:

import React from 'react'

function MapObj() {

    const obj = {
        items: [
          {
          id: 1,
          title: 'Pizza'
        },
        {
         id: 2,
         title: 'Hot-Dog'
       }
       ],
        total: 200,
        isEmpty: false
      };

  const mappedObj = obj.items.map(item => {
      return (
       <>
         <div key={item.id}>
          <h2>{item.title}</h2>
         </div>
        <p>{obj.total}</p>
        <p>{obj.isEmpty}</p>
       </>
      )
  })

  return (
    <div style={{textAlign: 'center', color: 'maroon'}}>
        {mappedObj}
    </div>
  )
}

export default MapObj
about 4 years ago · Juan Pablo Isaza Denunciar

0

You cannot apply the method map on an object. Try applying it on the array inside the object:

const mappedObj = obj.items.map(item => {
      return (
       <>
         <div key={item.id}>
          <h2>{item.title}</h2>
         </div>
        <p>{obj.total}</p>
        <p>{obj.isEmpty}</p>
       </>
      )
  })
about 4 years ago · Juan Pablo Isaza Denunciar

0

Object don't have map

You can use this approach :

const mappedObj = obj.items.map(item => {
      return (
       <>
         <div key={item.id}>
          <h2>{item.title}</h2>
         </div>
       </>
      )

return (
    <div style={{textAlign: 'center', color: 'maroon'}}>
        {mappedObj}

        <p>{obj.items.total}</p>
        <p>{obj.items.isEmpty}</p>
    </div>
  )


And you can also loop the object with for in loop

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