-2

I have the following data set and I want to access the x and y values of every single set. how can I get access x and y values ?

enter image description here

cham
  • 77
  • 2
  • 5

3 Answers3

3

The solution is very simple, you have to iterate the first array, each entry of this is another array so you have to iterate it too.

 for(const items of firstArray)  {
      for(const object of items) {
        console.log(object.x);
        console.log(object.y);
      }
    
    }

This solution is better to one that use forEach in terms of performance. https://betterprogramming.pub/should-you-stop-using-foreach-in-your-javascript-code-efe1e86c78e5

FedG
  • 1,218
  • 1
  • 5
  • 14
  • 2
    Code-only answers are not good. Please take the time to explain the code you have posted. Also, there are other ways of achieving this - explain to OP why you are recommending this way? – Martin May 12 '21 at 07:22
0

You can use any iterators to traverse through the array.

array.forEach(item => 
           item.forEach(
             sub => console.log(sub.x,sub.y)
           ));
Jackson
  • 35
  • 4
0

You can access the values of nested arrays in angular by using nested ngFor loops. It can be achieved by this.

 <div *ngFor="let item of Array1;let i = index" >                         
     <div *ngFor="let subItem of item[i]"> 
          <p>{{subItem.x}}</p> 
          <p>{{subItem.y}}</p> 
     </div>                             
 </div>
M.Hassan Nasir
  • 851
  • 2
  • 13