Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Reformar objeto en otro objeto

Estoy tratando de usar la biblioteca pro-gallery en mi proyecto y acepta una matriz de imágenes como esta:

 const items = [ { // Image item: itemId: "sample-id", mediaUrl: "https://i.picsum.photos/id/674/200/300.jpg?hmac=kS3VQkm7AuZdYJGUABZGmnNj_3KtZ6Twgb5Qb9ITssY", metaData: { type: "image", height: 10, width: 100, title: "sample-title", description: "sample-description", }, }, ... ]

Pero mi propia matriz de elementos se ve así, ¿cómo haría para extraer campos interesantes y recrear una nueva matriz?

 "products": [ { "_id": "618e2787d193ae4a3068cae5", "name": "Photo", "category": "cuba", "image": "https://someimage.com/dqve1.jpg", "price": 20, "countInStock": 12, "brand": "John Doe", "description": "Description example...", "seller": "618aeab0f82f5854ec925a5f", }, ... ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Supongo que le gustaría aplanar su objeto. Entonces, si entiendo la pregunta correctamente, esta solución debería funcionar perfectamente.

Puede usar forEach para iterar a través de su matriz de objetos, luego use Objects.entries para extraer los pares de valores clave de los objetos e insertarlos en un nuevo objeto en nuestro formato preferido. Finalmente, empujamos ese objeto a nuestra matriz.

 const items = [ { itemId: "sample-id", mediaUrl: "https://i.picsum.photos/id/674/200/300.jpg?hmac=kS3VQkm7AuZdYJGUABZGmnNj_3KtZ6Twgb5Qb9ITssY", metaData: { type: "image", height: 10, width: 100, title: "sample-title", description: "sample-description", }}, { itemId: "secondSample", mediaUrl: "https://i.picsum.photos/id/674/200/300.jpg?hmac=kS3VQkm7AuZdYJGUABZGmnNj_3KtZ6Twgb5Qb9ITssY", metaData: { type: "image", height: 100, width: 200, title: "second image", description: "sample-description", }}, { itemId: "sample-id-3", mediaUrl: "https://i.picsum.photos/id/674/200/300.jpg?hmac=kS3VQkm7AuZdYJGUABZGmnNj_3KtZ6Twgb5Qb9ITssY", metaData: { type: "image", height: 1028, width: 130, title: "sample-title-3", description: "sample-description-3", }, }, ]; let newItems = {"items": [ ]}; items.forEach((item, index) => { let thisItem = {}; for (const [key, value] of Object.entries(item)) { if(typeof value === 'object'){ for (const [k, v] of Object.entries(value)) { thisItem[k] = v; } } else { thisItem[key] = value; } } newItems.items.push(thisItem); }); console.log(newItems);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!