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

139
Vistas
Reacciona el objeto a la matriz para imprimir en la interfaz usando .map

en realidad he devuelto 2 tipos de resultados de 1 llamada API

 { "data1": [ { "item_name": "item 4", "price_updated_date": "2022-04-14T08:05:16.339Z", "item_img_id": "https://testing.jpg", "set_name": "2017", "set_tag": "mm3", "rarity": "rare", "price_normal": 34.99, "price_foil": 54.99 }, ], "data2": [ { "item_condition": "NM", "item_img": "https://test.jpg", "item_name": "item 1", "item_set": "ZEN", "item_language": "JP", "item_type": "Non Foil", "item_price": "¥ 4,580", "item_qty": 9, "other_condition": { "SP": { "qty": 1, "price": "¥ 3,670" }, "MP": { "qty": 1, "price": "¥ 3,210" } } }, { "item_condition": "NM", "item_img": "https://0010.jpg", "item_name": "item 3", "item_set": "ZNE", "item_language": "JP", "item_type": "Non Foil", "item_price": "¥ 8,000", "item_qty": 6, "other_condition": { "MP": { "qty": 1, "price": "¥ 6,400" } } }, ] }

desde el regreso, todos son Objeto, por lo que necesito hacer una conversión a Array para imprimirlos en mi interfaz con .map, así que uso esto

 const convertObjToArr = (responseObj) => { let arr = [] Object.keys(responseObj).forEach(function(key) { arr.push(responseObj[key]); }); return arr } convertObjToArr(data.data1) convertObjToArr(data.data2)

sin embargo, mi data2 tiene otro objeto dentro que también se llama "otra_condición" que no se está convirtiendo en Array y todavía está en Object. Cuando traté de mostrarlo con .map, me da error. En este caso, ¿cómo puedo manejar ese objeto en la matriz? ¿Hay alguna forma de convertirlos todos en Array independientemente de cuántos objetos anidados tenga en un solo dato?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

¿Esto ayuda?

 const renderThisItem = itm => { /*console.log( 'itm: ', itm, 'typeof: ', typeof itm, 'keys: ', Object.keys(itm) );*/ if (itm) { if (typeof itm === 'string' || typeof itm === 'number') { return (<div>{itm},</div>); } else if (Array.isArray(itm)) { return ( <div>[{ itm.map(it => renderThisItem(it)) }],</div> ) } else if (Object.keys(itm).length > 0) { return ( <div>{"{"} { Object.entries(itm) .map(([k, v]) => ( <div> <div class="val">{k}: {renderThisItem(v)}</div> </div> )) } {"},"}</div> ) } }; }; const Thingy = ({ dataObj, ...props }) => { return ( <div>{renderThisItem(dataObj)}</div> ); }; const rawData = { "data1": [ { "item_name": "item 4", "price_updated_date": "2022-04-14T08:05:16.339Z", "item_img_id": "https://testing.jpg", "set_name": "2017", "set_tag": "mm3", "rarity": "rare", "price_normal": 34.99, "price_foil": 54.99 }, ], "data2": [ { "item_condition": "NM", "item_img": "https://test.jpg", "item_name": "item 1", "item_set": "ZEN", "item_language": "JP", "item_type": "Non Foil", "item_price": "¥ 4,580", "item_qty": 9, "other_condition": { "SP": { "qty": 1, "price": "¥ 3,670" }, "MP": { "qty": 1, "price": "¥ 3,210" } } }, { "item_condition": "NM", "item_img": "https://0010.jpg", "item_name": "item 3", "item_set": "ZNE", "item_language": "JP", "item_type": "Non Foil", "item_price": "¥ 8,000", "item_qty": 6, "other_condition": { "MP": { "qty": 1, "price": "¥ 6,400" } } }, ] }; ReactDOM.render( <div> DEMO <Thingy dataObj={rawData} /> </div>, document.getElementById('rd') );
 .val { display: flex; }
 <div id="rd" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

about 4 years ago · Santiago Trujillo 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