# Lookup closest matching value of array and return array index number

**URL:** <https://discourse.nodered.org/t/lookup-closest-matching-value-of-array-and-return-array-index-number/32927>\
**Category:** General\
**Created:** [15 September 2020 21:03 UTC](https://discourse.nodered.org/t/lookup-closest-matching-value-of-array-and-return-array-index-number/32927 "2020-09-15T21:03:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zodiac-69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zodiac-69/32/29022_2.png) [@Zodiac-69](https://discourse.nodered.org/u/Zodiac-69)\
**Post date:** [15 September 2020 21:03 UTC](https://discourse.nodered.org/t/lookup-closest-matching-value-of-array-and-return-array-index-number/32927/1 "2020-09-15T21:03:56Z")

</div>

Hi All

I have a payload 0 to 5 in non-linear fractions. i have a "look up" array[16]  
How do i do the following?

1. Find the value in the array that closest match the payload.
2. Return the index position of this value.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [15 September 2020 21:19 UTC](https://discourse.nodered.org/t/lookup-closest-matching-value-of-array-and-return-array-index-number/32927/2 "2020-09-15T21:19:04Z")

</div>

Sounds like a google JavaScript problem.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [15 September 2020 21:42 UTC](https://discourse.nodered.org/t/lookup-closest-matching-value-of-array-and-return-array-index-number/32927/3 "2020-09-15T21:42:57Z")

</div>

Loop through your array and do some calculations and if statements

```auto
let result = array[0] // start with first value of array
let resultIndex = 0;

array.forEach((el, index) = > {

if difference of Math.abs(el - msg.payload) is less than the result difference .... 
result = el
resultIndex = index
})

```

**[Math.abs()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs)** for absolute value of number

Test flow :

```auto
[{"id":"e8ed68c4.d45f68","type":"function","z":"a311c276.8fe21","name":"","func":"let array = [1,2,4,-5,6,7,8,77,-4, 45]\nlet result = array[0] // start with first value of array\nlet resultIndex = 0;\n\n\narray.forEach((el, index) => {\n\nif (Math.abs(el - msg.payload) < Math.abs(result -msg.payload)) {\nresult = el;\nresultIndex = index;\n}\n})\nmsg.array = array;\nmsg.result = result;\nmsg.resultIndex = resultIndex;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":2260,"wires":[["cd666bd5.22db58"]]},{"id":"da3c450b.a19978","type":"inject","z":"a311c276.8fe21","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-6.2","payloadType":"num","x":300,"y":2260,"wires":[["e8ed68c4.d45f68"]]},{"id":"cd666bd5.22db58","type":"debug","z":"a311c276.8fe21","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":2260,"wires":[]}]

```

ps. it would be better if we had some real data to better test the results

---

<div class="post-metadata">

**Author:** ![Zodiac-69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zodiac-69/32/29022_2.png) [@Zodiac-69](https://discourse.nodered.org/u/Zodiac-69)\
**Post date:** [16 September 2020 12:19 UTC](https://discourse.nodered.org/t/lookup-closest-matching-value-of-array-and-return-array-index-number/32927/4 "2020-09-16T12:19:13Z")

</div>

Hi All  
Thank you for your time to assist.  
My function looks like this:

var myNumber = (5/15)\*msg.payload  
var numbers = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]  
var MyValue = Math.abs(numbers[0] - myNumber)  
var idx = 0  
for(var c = 1; c \< numbers.length; c++)  
{  
var cMyValue = Math.abs(numbers[c] - myNumber)  
if(cMyValue \< MyValue)  
{  
idx = c  
MyValue = cMyValue  
}  
}  
msg.payload = idx  
return msg

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 September 2020 12:19 UTC](https://discourse.nodered.org/t/lookup-closest-matching-value-of-array-and-return-array-index-number/32927/5 "2020-09-30T12:19:15Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
