在 TypeScript 中解析 JSON - 添加 'data' 标签
Parse JSON in TypeScript - add 'data' tag
JSON 看起来像这样:
[
{
"id":1,
"position":3,
"articleNumber":"ServiceElement"
},
{
"id":2,
"position":2,
"articleNumber":"ServiceElement"
}
]
有没有可能通过any的方式让它变成这样:
{
"data":[
{
"data":{
"id":1,
"position":3,
"articleNumber":"ServiceElement"
}
},
{
"data":{
"id":2,
"position":2,
"articleNumber":"ServiceElement"
}
}
]
}
我需要 data 标记来识别 TreeTable 实现的对象,但我给定的 JSON 不符合该对象。
只需使用 map
函数来改变项目的形状。
对于第一级,我们创建一个新对象 { data: }
,然后将 data
属性 array.map
.
的结果赋值给 data
属性
const array = [
{
"id":1,
"position":3,
"articleNumber":"ServiceElement"
},
{
"id":2,
"position":2,
"articleNumber":"ServiceElement"
}
];
const mapped = { data: array.map(item => ({ data: item }))};
console.log(mapped);
JSON 看起来像这样:
[
{
"id":1,
"position":3,
"articleNumber":"ServiceElement"
},
{
"id":2,
"position":2,
"articleNumber":"ServiceElement"
}
]
有没有可能通过any的方式让它变成这样:
{
"data":[
{
"data":{
"id":1,
"position":3,
"articleNumber":"ServiceElement"
}
},
{
"data":{
"id":2,
"position":2,
"articleNumber":"ServiceElement"
}
}
]
}
我需要 data 标记来识别 TreeTable 实现的对象,但我给定的 JSON 不符合该对象。
只需使用 map
函数来改变项目的形状。
对于第一级,我们创建一个新对象 { data: }
,然后将 data
属性 array.map
.
data
属性
const array = [
{
"id":1,
"position":3,
"articleNumber":"ServiceElement"
},
{
"id":2,
"position":2,
"articleNumber":"ServiceElement"
}
];
const mapped = { data: array.map(item => ({ data: item }))};
console.log(mapped);