Max size of array in function

I have a funtion, poster here - "Closest Array Match" with 1025 entries.
When i do my checks, the array index is always 1020 on the maximum.

I get the same resault with my function and with "UnborN" function that he posted.
Is 1020 the max array size, or am i doing something wrong?

const foo = new Array(1500)

for(var i = 0; i < foo.length; i++){
  foo[i] = Math.random()
}

msg.payload = foo

return msg;

image

So something would seem to be wrong in your code? Want to share?

Hi TotallyInformation

Thank you for that, see my test flow below.

[{"id":"3e70b103.f9c7de","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"a9acd99e.7d07d8","type":"ui_slider","z":"3e70b103.f9c7de","name":"","label":"slider","tooltip":"","group":"cf677043.b89ff","order":2,"width":0,"height":0,"passthru":false,"outs":"end","topic":"","min":0,"max":"150","step":1,"x":150,"y":220,"wires":[["ace43ab4.20e888","8fdfa66f.e42aa8"]]},{"id":"ace43ab4.20e888","type":"ui_gauge","z":"3e70b103.f9c7de","name":"","group":"cf677043.b89ff","order":1,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"kW","format":"{{value}}","min":0,"max":"150","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":910,"y":160,"wires":[]},{"id":"75fbd248.66bc9c","type":"function","z":"3e70b103.f9c7de","name":"","func":"msg.payload = (5/1024)*msg.payload\nlet array = [0, 0.004, 0.008, 0.012, 0.016, 0.02, 0.024, 0.028, 0.032, 0.036, 0.04, 0.0425, 0.045, 0.0475, 0.05, 0.0525, 0.055, 0.0575, 0.06, 0.0625, \n0.065, 0.0677, 0.0704, 0.0731, 0.0758, 0.0785, 0.0812, 0.0839, 0.0866, 0.0893, 0.092, 0.0945, 0.097, 0.0995, 0.102, 0.1045, 0.107, 0.1095, 0.112, 0.1145, \n0.117, 0.1195, 0.122, 0.1245, 0.127, 0.1295, 0.132, 0.1345, 0.137, 0.1395, 0.142, 0.1445, 0.147, 0.1495, 0.152, 0.1545, 0.157, 0.1595, 0.162, 0.1645, \n0.167, 0.1698, 0.1726, 0.1754, 0.1782, 0.181, 0.1838, 0.1866, 0.1894, 0.1922, 0.195, 0.1976, 0.2004, 0.2032, 0.206, 0.2088, 0.2116, 0.2144, 0.2172, 0.22, \n0.221, 0.22355, 0.2261, 0.2286, 0.2312, 0.2337, 0.2363, 0.2388, 0.2414, 0.2439, 0.2465, 0.249, 0.2515, 0.254, 0.2565, 0.259, 0.2615, 0.264, 0.2665, 0.269, \n0.2715, 0.274, 0.2769, 0.2798, 0.2827, 0.2856, 0.2885, 0.2914, 0.2943, 0.2972, 0.3001, 0.303, 0.3058, 0.3086, 0.3114, 0.3142, 0.317, 0.3198, 0.3226, 0.3254, \n0.3282, 0.331, 0.3337, 0.3364, 0.3391, 0.3418, 0.3445, 0.3472, 0.3499, 0.3526, 0.3553, 0.358, 0.3608, 0.3636, 0.3664, 0.3692, 0.372, 0.3748, 0.3776, 0.3804, \n0.3832, 0.386, 0.3891, 0.3922, 0.3953, 0.3984, 0.4015, 0.4046, 0.4077, 0.4108, 0.4139, 0.417, 0.4198, 0.4226, 0.4254, 0.4282, 0.431, 0.4338, 0.4366, 0.4394, \n0.4422, 0.445, 0.4475, 0.4501, 0.4526, 0.4552, 0.4577, 0.4603, 0.4628, 0.4654, 0.4679, 0.4705, 0.473, 0.4759, 0.4788, 0.4817, 0.4846, 0.4875, 0.4904, 0.4933, \n0.4962, 0.4991, 0.502, 0.5053, 0.5086, 0.5119, 0.5152, 0.5185, 0.5218, 0.5251, 0.5284, 0.5317, 0.535, 0.5378, 0.5406, 0.5434, 0.5462, 0.549, 0.5518, 0.5546, \n0.5574, 0.5602, 0.563, 0.5662, 0.5694, 0.5726, 0.5758, 0.579, 0.5822, 0.5854, 0.5886, 0.5918, 0.595, 0.5979, 0.6008, 0.6037, 0.6066, 0.6095, 0.6124, 0.6153, \n0.6182, 0.6211, 0.624, 0.6274, 0.6308, 0.6342, 0.6376, 0.641, 0.6444, 0.6478, 0.6512, 0.6546, 0.658, 0.6611, 0.6642, 0.6673, 0.6704, 0.6735, 0.6766, 0.6797, \n0.6828, 0.6859, 0.689, 0.6918, 0.6946, 0.6975, 0.7003, 0.7031, 0.7059, 0.7087, 0.7115, 0.7144, 0.7172, 0.72, 0.7232, 0.7264, 0.7296, 0.7328, 0.736, 0.7392, \n0.7424, 0.7456, 0.7488, 0.752, 0.7553, 0.7586, 0.7619, 0.7652, 0.7685, 0.7718, 0.7751, 0.7784, 0.7817, 0.785, 0.7884, 0.7918, 0.7952, 0.7986, 0.802, 0.8054, \n0.8088, 0.8122, 0.8156, 0.819, 0.8223, 0.8256, 0.8289, 0.8322, 0.8355, 0.8388, 0.8421, 0.8454, 0.8487, 0.852, 0.8553, 0.8586, 0.8619, 0.8652, 0.8685, 0.8718, \n0.8751, 0.8784, 0.8817, 0.885, 0.8884, 0.8918, 0.8952, 0.8986, 0.902, 0.9054, 0.9088, 0.9122, 0.9156, 0.919, 0.9226, 0.9262, 0.9298, 0.9334, 0.937, 0.9406, \n0.9442, 0.9478, 0.9514, 0.955, 0.9583, 0.9615, 0.9648, 0.9681, 0.9714, 0.9746, 0.9779, 0.9812, 0.9845, 0.9877, 0.991, 0.994, 0.997, 1, 1.003, 1.006, 1.009, \n1.012, 1.015, 1.018, 1.021, 1.024, 1.0276, 1.0312, 1.0348, 1.0384, 1.042, 1.0456, 1.0492, 1.0528, 1.0564, 1.06, 1.0639, 1.0678, 1.0717, 1.0756, 1.0795, 1.0834, \n1.0873, 1.0912, 1.0951, 1.099, 1.1025, 1.106, 1.1095, 1.113, 1.1165, 1.12, 1.1235, 1.127, 1.1305, 1.134, 1.1376, 1.1412, 1.1448, 1.1484, 1.152, 1.1556, 1.1592, \n1.1628, 1.1664, 1.17, 1.1739, 1.1778, 1.1817, 1.1856, 1.1895, 1.1934, 1.1973, 1.2012, 1.2051, 1.209, 1.2131, 1.2172, 1.2213, 1.2254, 1.2295, 1.2336, 1.2377, \n1.2418, 1.2459, 1.25, 1.2534, 1.2567, 1.2601, 1.2635, 1.2668, 1.2702, 1.2735, 1.2769, 1.2803, 1.2836, 1.287, 1.291, 1.295, 1.299, 1.303, 1.307, 1.311, 1.315, \n1.319, 1.323, 1.327, 1.3306, 1.3342, 1.3378, 1.3414, 1.345, 1.3486, 1.3522, 1.3558, 1.3594, 1.363, 1.3675, 1.372, 1.3765, 1.381, 1.3855, 1.39, 1.3945, 1.399, \n1.4035, 1.408, 1.4121, 1.4162, 1.4203, 1.4244, 1.4285, 1.4326, 1.4367, 1.4408, 1.4449, 1.449, 1.4529, 1.4568, 1.4607, 1.4646, 1.4685, 1.4724, 1.4763, 1.4802, \n1.4841, 1.488, 1.4919, 1.4958, 1.4997, 1.5036, 1.5075, 1.5114, 1.5153, 1.5192, 1.5231, 1.527, 1.5318, 1.5366, 1.5414, 1.5462, 1.551, 1.5558, 1.5606, 1.5654, \n1.5702, 1.575, 1.5787, 1.5825, 1.5862, 1.5899, 1.5936, 1.5974, 1.601, 1.6048, 1.6085, 1.612, 1.616, 1.6202, 1.6244, 1.6286, 1.6328, 1.637, 1.6412, 1.6454, \n1.6496, 1.6538, 1.658, 1.6624, 1.6668, 1.6712, 1.6756, 1.68, 1.6844, 1.6888, 1.6932, 1.6976, 1.702, 1.7063, 1.7106, 1.7149, 1.7192, 1.7235, 1.7278, 1.7321, \n1.7364, 1.7407, 1.745, 1.75, 1.755, 1.76, 1.765, 1.77, 1.775, 1.78, 1.785, 1.79, 1.795, 1.7995, 1.804, 1.8085, 1.813, 1.8175, 1.822, 1.8265, 1.831, 1.8355, \n1.84, 1.8444, 1.8488, 1.8532, 1.8576, 1.862, 1.8664, 1.8708, 1.8752, 1.8796, 1.884, 1.8886, 1.8932, 1.8978, 1.9024, 1.907, 1.9116, 1.9162, 1.9208, 1.9254, \n1.93, 1.9346, 1.939, 1.9439, 1.9485, 1.9532, 1.9578, 1.9625, 1.967, 1.9717, 1.9764, 1.981, 1.9857, 1.9904, 1.9951, 1.9998, 2.0045, 2.0092, 2.0139, 2.0186, \n2.0233, 2.028, 2.0327, 2.0374, 2.0421, 2.0468, 2.0515, 2.0562, 2.0609, 2.0656, 2.0703, 2.075, 2.0799, 2.0848, 2.0897, 2.0946, 2.0995, 2.1044, 2.1093, 2.1142, \n2.1191, 2.124, 2.1294, 2.1348, 2.1402, 2.1456, 2.151, 2.1564, 2.1618, 2.1672, 2.1726, 2.178, 2.1831, 2.1882, 2.1933, 2.1984, 2.2035, 2.2086, 2.2137, 2.2188, \n2.2239, 2.229, 2.2341, 2.2392, 2.2443, 2.2494, 2.2545, 2.2596, 2.2647, 2.2698, 2.2749, 2.28, 2.2851, 2.2902, 2.2953, 2.3004, 2.3055, 2.3106, 2.3157, 2.3208, \n2.3259, 2.331, 2.3363, 2.3415, 2.3468, 2.3521, 2.3574, 2.3626, 2.3679, 2.3732, 2.3784, 2.3837, 2.389, 2.3943, 2.3996, 2.4049, 2.4102, 2.4155, 2.4208, 2.4261, \n2.4314, 2.4367, 2.442, 2.4471, 2.4522, 2.457, 2.4624, 2.4675, 2.4725, 2.4776, 2.4827, 2.4878, 2.4929, 2.498, 2.503, 2.508, 2.513, 2.518, 2.523, 2.528, 2.533, \n2.538, 2.543, 2.548, 2.5543, 2.5606, 2.5669, 2.5732, 2.5795, 2.5858, 2.5921, 2.5984, 2.6047, 2.611, 2.6167, 2.6224, 2.6281, 2.6338, 2.6395, 2.6452, 2.6509, \n2.6566, 2.6623, 2.668, 2.6737, 2.6794, 2.6851, 2.6908, 2.6965, 2.7022, 2.7079, 2.7136, 2.7193, 2.725, 2.7306, 2.7362, 2.7418, 2.7474, 2.753, 2.7586, 2.7642, \n2.7698, 2.7754, 2.781, 2.7871, 2.7932, 2.7993, 2.8054, 2.8115, 2.8175, 2.8236, 2.8297, 2.8358, 2.8419, 2.848, 2.8539, 2.8598, 2.8657, 2.8716, 2.8775, 2.8834, \n2.8893, 2.8952,2.9011, 2.907, 2.9132, 2.9194, 2.9256, 2.9318, 2.938, 2.9442,2.9504, 2.9566, 2.9628, 2.969, 2.9752, 2.9814, 2.9876, 2.9938, 3, 3.0062, 3.0124, \n3.0186, 3.0248, 3.031, 3.0373, 3.0436, 3.0499, 3.0562, 3.0625, 3.0688, 3.0751,3.0814, 3.0877, 3.094, 3.1008, 3.1076, 3.1144, 3.1212, 3.128, 3.1348, 3.1416, \n3.1484, 3.1552, 3.162, 3.1685, 3.175, 3.1815, 3.188, 3.1945, 3.201, 3.2075, 3.214, 3.2205, 3.227, 3.2338, 3.2406, 3.2474, 3.2542, 3.261, 3.2678, 3.2746, 3.2814, \n3.2882, 3.295, 3.301, 3.307, 3.313, 3.319, 3.325, 3.331, 3.337, 3.343, 3.349, 3.355, 3.361, 3.3686, 3.3762, 3.3838, 3.3914, 3.399, 3.4066, 3.4142, 3.4218, \n3.4294, 3.437, 3.4438, 3.4506, 3.4574, 3.4642, 3.471, 3.4778, 3.4846, 3.4914, 3.4982, 3.505, 3.5123, 3.5196, 3.5269, 3.5342, 3.5415, 3.5488, 3.5561, 3.5634, \n3.5707, 3.578, 3.5853, 3.6218, 3.6291, 3.6364, 3.6437, 3.651, 3.6588, 3.6666, 3.6744, 3.6822, 3.69, 3.6978, 3.7056, 3.7134, 3.7212, 3.729, 3.7361, 3.7432, \n3.7503, 3.7574, 3.7645, 3.7716, 3.7787, 3.7858, 3.7929, 3.8, 3.8079, 3.8158, 3.8237, 3.8316, 3.8395, 3.8474, 3.8553, 3.8632, 3.8711, 3.879, 3.8862, 3.893, \n3.9005, 3.9077, 3.9149, 3.9221, 3.929, 3.9365, 3.9436, 3.9508, 3.958, 3.9666, 3.9752, 3.9838, 3.9924, 4.001, 4.0096, 4.0182, 4.0268, 4.0354, 4.044, 4.0516, \n4.0592, 4.0668, 4.0744, 4.082, 4.0896, 4.0972, 4.1048, 4.1124, 4.12, 4.129, 4.138, 4.147, 4.156, 4.165, 4.174, 4.183, 4.192, 4.201, 4.21, 4.218, 4.226, 4.234, \n4.242, 4.25, 4.258, 4.266, 4.274, 4.282, 4.29, 4.299, 4.308, 4.317, 4.326, 4.335, 4.344, 4.353, 4.362, 4.371, 4.38, 4.388, 4.396, 4.404, 4.412, 4.42, 4.428, \n4.436, 4.444, 4.452, 4.46, 4.469, 4.478, 4.487, 4.496, 4.505, 4.514, 4.523, 4.532, 4.541, 4.55, 4.5582, 4.5664, 4.5745, 4.5827, 4.5909, 4.5991, 4.6073, \n4.6155, 4.6236, 4.6318, 4.64, 4.65, 4.66, 4.67, 4.68, 4.69, 4.7, 4.71, 4.72, 4.73, 4.74, 4.752, 4.764, 4.776, 4.788, 4.8, 4.812, 4.824, 4.836, 4.848, 4.86, 4.869, \n4.878, 4.887, 4.896, 4.905, 4.914, 4.923, 4.932, 4.941, 4.95, 4.955, 4.96, 4.965, 4.97, 4.975, 4.98, 4.985, 4.99, 4.995, 5]\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;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":220,"wires":[["14d47e3f.d0af32","e2493e5b.b914c"]]},{"id":"824360ff.bdaa9","type":"debug","z":"3e70b103.f9c7de","name":"Slider","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":950,"y":500,"wires":[]},{"id":"14d47e3f.d0af32","type":"ui_text","z":"3e70b103.f9c7de","group":"cf677043.b89ff","order":1,"width":0,"height":0,"name":"","label":"text","format":"{{msg.resultIndex}}","layout":"row-left","x":940,"y":220,"wires":[]},{"id":"e2493e5b.b914c","type":"debug","z":"3e70b103.f9c7de","name":"Calc","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":950,"y":420,"wires":[]},{"id":"8fdfa66f.e42aa8","type":"range","z":"3e70b103.f9c7de","minin":"0","maxin":"150","minout":"0","maxout":"1024","action":"clamp","round":true,"property":"payload","name":"","x":420,"y":220,"wires":[["75fbd248.66bc9c","824360ff.bdaa9"]]},{"id":"cf677043.b89ff","type":"ui_group","z":"","name":"Test","tab":"43958b31.dce714","order":1,"disp":true,"width":"6","collapse":false},{"id":"43958b31.dce714","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

If you look at the debug message, it seems to only see an array of [1021] and not the full 1025

Do you realize you only have 1021 values in the function node array you build?

:flushed:
coppied from excel - 1025 cells - did not count them in the flow.
:flushed:

1 Like

If you paste the flow into a text editor, delete the bits round the array so you just have the numbers left and then search for comma you will find (assuming that your editor tells you how many it finds, as gedit does for example) that there are 1020 of them, so that does suggest that @zenofmud is correct, 1021 values.
Were there some empty cells in the spreadsheet possibly?

Hi All
Re-entered the information, and now i get 1025, not sure what went wrong...PEBKAC...
image

2 Likes

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