typescript使用解构体方法处理该json数组{"data":[{"id":"1234"},{"name":"他"},{"age":"11"},{"number":"987654"},{"tag":[{"test":"1"},{"test":"2"}]}]}
时间: 2023-11-30 10:03:00 浏览: 27
以下是使用解构体方法处理该JSON数组的TypeScript代码:
```typescript
interface Data {
id: string;
name: string;
age: string;
number: string;
tag: Tag[];
}
interface Tag {
test: string;
}
const data: Data = {
id: "",
name: "",
age: "",
number: "",
tag: [{ test: "" }],
};
const { data: [{ id }, { name }, { age }, { number }, { tag: [{ test }] }] } = {
data: [
{ id: "1234" },
{ name: "他" },
{ age: "11" },
{ number: "987654" },
{ tag: [{ test: "1" }, { test: "2" }] },
],
};
data.id = id;
data.name = name;
data.age = age;
data.number = number;
data.tag[0].test = test;
console.log(data);
```
输出结果为:
```typescript
{
id: '1234',
name: '他',
age: '11',
number: '987654',
tag: [ { test: '1' } ]
}
```
阅读全文