Anand Trivedi
Anand Trivedi

Reputation: 123

Call function on each array indexs value

I have an array which looks like this

 arcAxis:
0:{x: 1.2858791391047208e-15, y: 21}
1:{x: -21, y: 21.000000000000004}
2:{x: -35.8492424049175, y: 6.150757595082504}
3:{x: -39.40038395815852, y: -14.546812157640753}
4:{x: -32.12697787933814, y: -34.24700413672001}
5:{x: -16.811252024253655, y: -48.61462542668643}
6:{x: 3.0355856977321465, y: -55.47779032614515}

Now I have a function which draws elements using x and y of arcAxis.

What I want to do is to call that function to draw an element for each of arcAxis's index value something like this

function test() {
  plot(0. x, 0. y)
}
.....

function test() {
  plot(6. x, 6. y)
}

So, that I have 6 new elements made on different x,y values respective to their indexes

my approach is printing each element 6 times then printing next element 6 times

function test() {
  const arcAxis = this.spiral();
  for (var z in arcAxis) {
    plot(arcAxis[z].x, arcAxis[z].x)

  }
}

Anyway, can I print each element only 1 time with only 1 indexes value?

Upvotes: 0

Views: 94

Answers (2)

Mohammed Ashfaq
Mohammed Ashfaq

Reputation: 3426

let data= {
  arcAxis:[
   {x: 1.2858791391047208e-15, y: 21},
   {x: -21, y: 21.000000000000004},
   {x: -35.8492424049175, y: 6.150757595082504},
   {x: -39.40038395815852, y: -14.546812157640753},
   {x: -32.12697787933814, y: -34.24700413672001},
   {x: -16.811252024253655, y: -48.61462542668643},
   {x: 3.0355856977321465, y: -55.47779032614515}
  ]
 }
 
 data.arcAxis.forEach(({x, y})=>{ 
   plot(x,y);
 })
 
function plot(x,y){
  console.log("X: ", x,"Y: ", y );
}

Upvotes: 1

senjin.hajrulahovic
senjin.hajrulahovic

Reputation: 3191

If you are using a pre EcmaScript 6 version:

arcAxis.forEach(function(element) {
    plot(element.x, element.y);
});

And for EcmaScript 6 and onwards:

arcAxis.forEach(element => plot(element.x, element.y));

enter image description here

Upvotes: 0

Related Questions