简体   繁体   English

在每个数组的索引值上调用函数

[英]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. 现在,我有了一个使用arcAxis的x和y绘制元素的函数。

What I want to do is to call that function to draw an element for each of arcAxis's index value something like this 我想做的就是调用该函数为arcAxis的每个索引值绘制一个元素,如下所示

 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 所以,我有6个新元素,分别针对它们的索引在不同的x,y值上

my approach is printing each element 6 times then printing next element 6 times 我的方法是将每个元素打印6次,然后再打印下一个元素6次

 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? 无论如何,我只能使用1个索引值仅打印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 ); } 

If you are using a pre EcmaScript 6 version: 如果您使用的是EcmaScript 6之前的版本:

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

And for EcmaScript 6 and onwards: 对于EcmaScript 6及更高版本:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM