0

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?

6
  • I don’t have any way to test, and I don’t know much Typescript, but I’m pretty sure you need to add parts = [] after you push parts onto the supplier. Otherwise, you keep reusing the same array for all suppliers, and tying the same parts array to all suppliers. Note, even after you add a parts array to a supplier, if you push more onto that array, the supplier will now have those parts, because you mutated the array. An even better solution would be to get a new parts array at the beginning of each loop iteration. Commented Oct 9, 2018 at 2:34
  • I think I understood what you mean, but I do not know much how to implement this. Commented Oct 9, 2018 at 3:14
  • As a very simple proof of concept, change the word const to let, in your ‘const parts: Part[] = [];` line. That will allow you set it to a new array later. Then, right after the suppliers.push(supplier); add parts = [];. This will give you a fresh array for your next supplier. Commented Oct 9, 2018 at 3:20
  • @krishna just posted the alternative (better) example I was trying to describe, which is to have a fresh array for each supplier from the get go. Commented Oct 9, 2018 at 3:22
  • collection[key] shouldn’t that be collection[i][j] ? Commented Oct 9, 2018 at 3:24

1 Answer 1

1
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
)

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.