Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

550
Visualizações
TypeScript how to format array of object to array of json

I have posted data like _attachments variable: enter image description here

I want to prepare that data to insert as the following structure:

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

But what i have done:

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'];
}

give this error:

 Unprocessable Entity

Note: I'm using loopback.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Just add this to your attachment.josn:

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

and no need to loop the data.

over 4 years ago · Santiago Trujillo Relatório

0

From the screen shot _attachments seems to be an array; if that is that case you should not use for...in to iterate over it but for..of. for..in will return all enumerable properties including potentially "unwanted" ones

See the bottom of this excellent MDN resource for details (for..of is available in Typescript)

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

Even better, use a map

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

Also the structure you map to seem identical to the original structure, why not use a direct assignment ?

over 4 years ago · Santiago Trujillo Relatório

0

You need to Stringify it.

Code

data = { 
    container: "Stuff"
}

var jsonString = JSON.stringify(data); 

console.log(jsonString);

Before

enter image description here

After

enter image description here

This is more what your data looks like,

enter image description here

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda