• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

307
Views
TypeScript cómo formatear una matriz de objeto a una matriz de json

He publicado datos como la variable _attachments : ingrese la descripción de la imagen aquí

Quiero preparar esos datos para insertarlos como la siguiente estructura:

 "_attachments": [ { "container": "string", "fileName": "string", "name": "string", "mime": "string", "size": 0 } ]

Pero lo que he hecho:

 for(let key in _attachments) { job._attachments[key]['container'] = _attachments[key]['container']; job._attachments[key]['fileName'] = _attachments[key]['fileName']; job._attachments[key]['name'] = _attachments[key]['name']; job._attachments[key]['mime'] = _attachments[key]['mime']; job._attachments[key]['size'] = _attachments[key]['size']; }

dar este error:

 Unprocessable Entity

Nota: estoy usando loopback.

almost 3 years ago · Santiago Trujillo
3 answers
Answer question

0

Simplemente agregue esto a su attachment.josn :

 "id": { "type": "string", "id": true, "defaultFn": "uuid" }

y no hay necesidad de hacer un bucle de datos.

almost 3 years ago · Santiago Trujillo Report

0

Desde la captura de pantalla, _attachments parece ser una matriz; si ese es el caso, no debe usar for...in para iterar sobre él, sino for..of . for..in devolverá todas las propiedades enumerables, incluidas las potencialmente "no deseadas"

Consulte la parte inferior de este excelente recurso de MDN para obtener detalles ( for..of está disponible en Typescript)

 for (let index of _attachments) { ... }

Aún mejor, usa un mapa

 const result = _attachments.map( att => ...)

Además, la estructura que mapea para que parezca idéntica a la estructura original, ¿por qué no usar una asignación directa?

almost 3 years ago · Santiago Trujillo Report

0

Necesitas Stringificarlo.

Código

 data = { container: "Stuff" } var jsonString = JSON.stringify(data); console.log(jsonString);

Antes

ingrese la descripción de la imagen aquí

Después

ingrese la descripción de la imagen aquí

Esto es más como se ven sus datos,

ingrese la descripción de la imagen aquí

almost 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error