0

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?

6
  • 3
    Unsure what you mean with your last question "anyway i can print each element only 1 time with only 1 indexes value?". Do you have ONE array with SIX elements, or SIX arrays with SIX elements? If you have only one array, you can plot its elements 6 times if you wish, but you will plot 6 times the same elements. Commented Sep 19, 2018 at 7:26
  • @minitauros Hi, what i want to do is take the 0th indexs value plot 1st element regarding those values, then take 1st indexes value and plot 2nd element regarding those value. Commented Sep 19, 2018 at 7:28
  • What about a simple for() loop? E.g for (let i = 0; i < arr.length; i++) { plot(arr[(i + 1)]); } (small example because I cannot do newlines in a comment, but hopefully you get the gist). In my example I use the element at index i + 1, which is not the current, but the next element. Commented Sep 19, 2018 at 7:30
  • @minitauros Hi i tried using for loop. Its printing 1st element 6 times then 2nd one 6 times and so on Commented Sep 19, 2018 at 7:36
  • 1
    You should edit your answer to clarify your starting data. And also clarify your intent. Currently it's nor really clear. Commented Sep 19, 2018 at 7:41

2 Answers 2

1

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 );
}

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

1 Comment

Nice usage of object destructuring.
0

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

1 Comment

hi I tried ur way, but its printing each element at only the last x and ys value

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.