# Getting nearest value from an array (arrays again...)

**URL:** <https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397>\
**Category:** classic bestof\
**Created:** [February 7, 2002, 1:44pm UTC](https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397 "2002-02-07T13:44:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 7, 2002, 1:44pm UTC](https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397/1 "2002-02-07T13:44:35Z")

</div>

I have an array set up with a list of values which are actualy button positions in a movie clip…I know how to get the first or last values of the array, but what I need help on is creating a code to get the \_x position of the clip and then access the array list to jump to the nearest button position in the movie clip…I hope I’m making sence…\r\rThanx 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2002, 3:59am UTC](https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397/2 "2002-02-08T03:59:07Z")

</div>

```auto
 \r\rArray.prototype.getnearestto = function(v){\r\r var n,r,diff,t;\r\r n = r = this.length-1;\r\r diff = Math.abs(v-this[n]);\r\r while(n--){\r\r t = Math.abs(v-this[n]);\r\r if(t<diff){ r=n; diff=t; }\r\r }\r\r return(this[r]);\r\r}\r\r

```

\rusage:

```auto
 \r\rvalues = [37,5,8,87,324,6,36,43,59];\r\rtest = values.getnearestto(100); // test = 87;

```

\ri wasn’t quite sure what you wanted, but this you should be able to modify this to do what you want.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 11, 2002, 2:34pm UTC](https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397/3 "2002-02-11T14:34:42Z")

</div>

Here is the code I am attempting to use:…\r\r&nbsp &nbsp &nbsp &nbsp pos = new Array(“2”, “102”, “208”, “308”, “410”, “514”, “620”, “726”, “830”, “934”);\r\r&nbsp &nbsp &nbsp &nbsp z = getProperty(“movieclip”, \_x);\r&nbsp &nbsp &nbsp &nbsp \rdo {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp a = a+1;\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp y = (pos[a]);\r&nbsp &nbsp &nbsp &nbsp } while (y\<=z);\r&nbsp &nbsp &nbsp &nbsp \rsetProperty (“movieclip”, \_x, -y);

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 11, 2002, 7:55pm UTC](https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397/4 "2002-02-11T19:55:17Z")

</div>

make sure that you define getnearestto before running the code, it only needs to be defined once to exist.\r

```auto
 \r\rmovieclip._x = pos.getnearesto(movieclip._x);

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 12, 2002, 2:18pm UTC](https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397/5 "2002-02-12T14:18:18Z")

</div>

I still cant get the code to work ☹ !\r\rHere’s the code I’m using\ron (rollOver) {\r&nbsp &nbsp &nbsp &nbsp getnearestto = function (v) { var n, r, diff, t;n = r=this.length-1;diff = Math.abs(v-this[n]);while (n–) {t = Math.abs(v-this[n]);if (t\<diff) {r = n;diff = t;}}};\r&nbsp &nbsp &nbsp &nbsp tellTarget (“striponstage”) {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp gotoAndPlay (5);\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp pos = new Array(“2”, “102”, “208”, “308”, “410”, “514”, “620”, “726”, “830”, “934”, “1040”, “1144”, “1248”, “1354”, “1458”, “1562”, “1666”, “1768”, “1872”, “1980”, “2084”, “2186”, “2292”);\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp clipposit = getProperty(“striponstage.strip”, \_x);\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp z = pos.getnearesto(clipposit);\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp setProperty (“striponstage.strip”, \_x, -(z));\r&nbsp &nbsp &nbsp &nbsp }\r}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 12, 2002, 6:11pm UTC](https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397/6 "2002-02-12T18:11:45Z")

</div>

Well, one thing wrong for sure is that you define your function everytime you rollover the button. Put that code somewhere else in the clip, in a frame or something. I think that this is not why your clip is acting wrong, but it’s a start.\r\rpom 0]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 12, 2002, 6:22pm UTC](https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397/7 "2002-02-12T18:22:07Z")

</div>

i think it’s returning zero, not the first array item.\r\ryou seem to have changed the prototype i put in my first post. it’s not returning anything anymore and you’ve removed the Array.prototype from the first line, it’s important to leave those in. there’s no need to define it every rollover, just once will suffice.\r\rcut and paste the definition in my first post into somewhere at the beginning of your movie where it will only run once. don’t forget to include the Array.prototype bit.\r\rdon’t put quotes around your numbers in the array, that’s making them strings and they’ll work better as numbers.\r\rotherwise, looks good! should work.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2002, 2:32pm UTC](https://forum.kirupa.com/t/getting-nearest-value-from-an-array-arrays-again/3397/8 "2002-06-02T14:32:35Z")

</div>

Just for the record :

```auto
 But here's something that should work :\r\rArray.prototype.getMinimum = function(){\r\r var n,min,i;\r\r n = this.length-1;\r\r min = this[0];\r\r for (i=0;i<n;i++) {\r\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp if (this[i]<min)\r\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp min = this[i];\r\r }\r\r return(min);\r\r}\r\r\r\rvalues = [37,5,8,87,-5,324,6,36,43,59];\r\rtest = values.getMinimum();\r\rtrace (test) ;

```

\r\rpom 0]
