2

I have this JSON object, It shows inside arrays as Json objects. so I can't iterate over them. any help would be appreciated. Below picture shows the types showing on vs code.I have initialized placeTypes as [] but it shows as {}.

InitialModel = [
    {nearByTypeName : "ex 1",id: 1,placeTypes:[
      {placeTypeId:1,placeTypeName:"ex1.1",
      places:[]}
    ]},
    {nearByTypeName : "ex 2",id: 2,placeTypes:[
      {id:2,placeTypeName:"ex 2.1",places:[]},
      {id:3,placeTypeName:"ex2.1.1",places:[]}
    ]}
  ]

enter image description here

enter image description here

4
  • 1
    It actually IS an array. look at the }[]; below places: any[]; Commented Nov 20, 2018 at 10:14
  • but have can i iterate.Can't use foreach loop? Commented Nov 20, 2018 at 10:15
  • Please show me your for loop Commented Nov 20, 2018 at 10:17
  • It's foreach inside foreach. i have added tha picture. Commented Nov 20, 2018 at 10:21

2 Answers 2

3

Try like this:

const initialModel = [
      {
        nearByTypeName: "ex 1", id: 1, placeTypes: [
          {
            placeTypeId: 1, placeTypeName: "ex1.1",
            places: []
          }
        ]
      },
      {
        nearByTypeName: "ex 2", id: 2, placeTypes: [
          { id: 2, placeTypeName: "ex 2.1", places: [] },
          { id: 3, placeTypeName: "ex2.1.1", places: [] }
        ]
      }
    ];


    for (const i of initialModel) {
      for (const j of i.placeTypes) {
        // do stuff
      }
    }

Edit: You stated you want to use foreach loops but cannot because of the following error:

Error: Cannot invoke an expression whose type lacks a call signature

That error is caused by a mismatch in types. It's caused by the difference in property names. if u rename id in the 2nd element of array to placeTypeId it works

 const initialModel = [
  {
    nearByTypeName: "ex 1", id: 1, placeTypes: [
      { placeTypeId: 1, placeTypeName: "ex1.1", places: [] }
    ]
  },
  {
    nearByTypeName: "ex 2", id: 2, placeTypes: [
      { placeTypeId: 2, placeTypeName: "ex 2.1", places: [] },
      { placeTypeId: 3, placeTypeName: "ex2.1.1", places: [] }
    ]
  }
];


initialModel.forEach(element => {
  const temp = element.placeTypes;
  temp.forEach(val => {
    // do stuff
  })
});
Sign up to request clarification or add additional context in comments.

4 Comments

why can't use double foreach loops?
@Asanka glad I could help! 👍
I think it is better to create a interface for the data so you will catch this error easily , +1 for catch this @TeunvanderWijst 👌
@malbarmawi Yeah I agree.
2

this how you can loop throw with nested loopes

component

  InitialModel = [
    {nearByTypeName : "ex 1",id: 1,placeTypes:[
      {placeTypeId:1,placeTypeName:"ex1.1",
      places:[
        this.deepCopy()
      ]}
    ]},
    {nearByTypeName : "ex 2",id: 2,placeTypes:[
      {id:2,placeTypeName:"ex 2.1",places:[
        this.deepCopy()
      ]},
      {id:3,placeTypeName:"ex2.1.1",places:[
        this.deepCopy()
      ]}
    ]}
  ]

  deepCopy(){
    return ['TRY','USA'].slice(0)
  }

Make sure deepCopy return array

template

<div *ngFor="let m of InitialModel">
    {{m.nearByTypeName }}

    <div *ngFor="let type of m.placeTypes">
        {{type.placeTypeName}}

        <div *ngFor="let place of type.places">
            {{place}}
        </div>
    </div>
</div>

demo

3 Comments

I want loop over in .ts file. it's work in html file.
can you check the demo and compare it with you code @Asanka
your code works on html. but I want loop in .ts file. that is the problem.

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.