TypeScript如何将对象数组格式化为json数组

时间:2022-03-16 21:42:21

I have posted data like _attachments variable: TypeScript如何将对象数组格式化为json数组

我发布了像_attachments变量这样的数据:

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.

注意:我正在使用环回。

4 个解决方案

#1


4  

Just add this to your attachment.josn:

只需将此添加到您的attachment.josn:

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

and no need to loop the data.

而且无需循环数据。

#2


3  

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

从屏幕截图来看_attachments似乎是一个数组;如果是那种情况你不应该使用... in迭代它但是for..of。 for..in将返回所有可枚举的属性,包括可能“不需要的”属性

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

有关详细信息,请参阅此优秀MDN资源的底部(for..of可在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 ?

您映射的结构看起来与原始结构相同,为什么不使用直接赋值?

#3


0  

You need to Stringify it.

你需要Stringify它。

Code

data = { 
    container: "Stuff"
}

var jsonString = JSON.stringify(data); 

console.log(jsonString);

Before

之前

TypeScript如何将对象数组格式化为json数组

After

TypeScript如何将对象数组格式化为json数组

This is more what your data looks like,

这更像是您的数据,

TypeScript如何将对象数组格式化为json数组

#4


0  

JSON.stringify will help you to get object in json format and JSON.parse will convert back from JSON to object.

JSON.stringify将帮助您以json格式获取对象,JSON.parse将从JSON转换回对象。

#1


4  

Just add this to your attachment.josn:

只需将此添加到您的attachment.josn:

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

and no need to loop the data.

而且无需循环数据。

#2


3  

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

从屏幕截图来看_attachments似乎是一个数组;如果是那种情况你不应该使用... in迭代它但是for..of。 for..in将返回所有可枚举的属性,包括可能“不需要的”属性

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

有关详细信息,请参阅此优秀MDN资源的底部(for..of可在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 ?

您映射的结构看起来与原始结构相同,为什么不使用直接赋值?

#3


0  

You need to Stringify it.

你需要Stringify它。

Code

data = { 
    container: "Stuff"
}

var jsonString = JSON.stringify(data); 

console.log(jsonString);

Before

之前

TypeScript如何将对象数组格式化为json数组

After

TypeScript如何将对象数组格式化为json数组

This is more what your data looks like,

这更像是您的数据,

TypeScript如何将对象数组格式化为json数组

#4


0  

JSON.stringify will help you to get object in json format and JSON.parse will convert back from JSON to object.

JSON.stringify将帮助您以json格式获取对象,JSON.parse将从JSON转换回对象。