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 ?
Asked
Active
Viewed 2,649 times
-2
-
1Did you try _anything_ before asking a question here? A quick Google search produced pages of useful articles on how to do this – Martin May 12 '21 at 07:09
-
you can do a nested loop using ngFor try doing a google search once – Akshay Bhat May 12 '21 at 07:12
-
1https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – Giovanni Esposito May 12 '21 at 07:12
3 Answers
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
-
2Code-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