Reputation: 39
I have multidimensional JSON that I need to iterate and adapt to a typescript object formatted for my view.
{
"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
}
]
}
}
}
}
How to iterate this JSON by separating items according to their respective suppliers.
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
)
}
I'm doing it that way, but all the suppliers are getting all the pieces.
Could someone give me a light to iterate this JSON in the correct way?
Upvotes: 0
Views: 227
Reputation: 151
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
)
}
Upvotes: 1