如何向 JSON 对象属性添加引号,例如:
{name:"User 01"}
Run Code Online (Sandbox Code Playgroud)
所以它应该看起来像这样:
{"name":"User 01"}
Run Code Online (Sandbox Code Playgroud)
它们都是字符串
如何使用TypeScript访问数组中不同文件中的多个类?
文件夹结构:
??? index.ts
??? models
??? index.ts
??? image.entity.ts
??? user.entity.ts
Run Code Online (Sandbox Code Playgroud)
image.entity.ts:
export class Image { }
Run Code Online (Sandbox Code Playgroud)
user.entity.ts:
export class User { }
Run Code Online (Sandbox Code Playgroud)
型号/index.ts:
export * from './image.entity';
export * from './user.entity';
Run Code Online (Sandbox Code Playgroud)
index.ts(所需输出):
import * as models from './models/index';
// Is an iterable array, instead of a "module"
console.log(models.length) // 2
Run Code Online (Sandbox Code Playgroud) 我想迫使一个号码是一个浮动,JSON.stringify()之后。不幸的是JSON.stringify()删除了1 .0。
范例:
JSON.stringify(1.0) // "1"
Run Code Online (Sandbox Code Playgroud)
想要的结果:
JSON.stringify(1.0) // "1.0"
Run Code Online (Sandbox Code Playgroud)
我正在使用一个API,该API需要JSON格式的对象,但它仅能理解十进制值。所以我想问问是否可以使用JSON.stringify生成带有十进制值的字符串而无需使用Regex-.replace-magic
我有以下功能:
function doThing(shouldReturnObject: boolean): string | object {
return shouldReturnObject ? { hello: 'world' } : 'hello world';
}
Run Code Online (Sandbox Code Playgroud)
我希望返回值在shouldReturnObject
equals为true 时为对象,但在为false时为字符串.