gpt4 book ai didi

javascript - Typescript 迭代多维 json

转载 作者:行者123 更新时间:2023-12-03 00:58:32 24 4
gpt4 key购买 nike

我有多维 JSON,需要迭代并适应为我的 View 格式化的 typescript 对象。

{
"data": {
"id": "71",
"type": "quotations",
"attributes": {
"parts": {
"name supplier 1": [
{
"id": 1,
"part_number": "10-09800",
"title": "FALCON ACCELEROMETER G METER -5G TO +10G",
"supplier_id": 1
},
{
"id": 3,
"part_number": "1999",
"title": "Peça teste",
"supplier_id": 1
}
],
"name supplier 2": [
{
"id": 2,
"part_number": "10-09800",
"title": "FALCON ACCELEROMETER G METER -5G TO +10G",
"supplier_id": 2
}
]
}
}
}
}

如何通过根据各自的 vendor 分隔项目来迭代此 JSON。

private responseToQuotationSuppliers(response: Response): QuotationSupplier {
const collection = response.json().data.attributes['parts'];
const suppliers: QuotationSupplierPart[] = [];
const parts: Part[] = [];

for (const i in collection) {
for (const j in collection[i]) {
collection[key].forEach(item => {
let part = new Part(
item.id,
item.part_number,
item.tile,
item.supplier_id
)
parts.push(part);
})
}

let supplier = new QuotationSupplierPart(
key,
parts
)
suppliers.push(supplier);
}

return new QuotationSupplier(
suppliers,
response.json().data.id
)
}

我就是这样做的,但所有 vendor 都得到了所有的东西。

有人能给我一个启发,让我以正确的方式迭代这个 JSON 吗?

最佳答案

private responseToQuotationSuppliers(response: Response): QuotationSupplier {
const collection = response.json().data.attributes['parts'];
const suppliers: QuotationSupplierPart[] = [];


for (const i in collection) {
let parts: Part[] = [];
for (const j in collection[i]) {
collection[key].forEach(item => {
let part = new Part(
item.id,
item.part_number,
item.tile,
item.supplier_id
)
parts.push(part);
})
}

let supplier = new QuotationSupplierPart(
key,
parts
)
suppliers.push(supplier);
}

return new QuotationSupplier(
suppliers,
response.json().data.id
)

}

关于javascript - Typescript 迭代多维 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com