Problem with data in array

Hello guys, hope you doing well. I have a rare situaction on handling arrays, hope you can help me. The issue itself is that am using two arrays, one main and another to store certain amount of data from the fisrt one like a stack, wich runs all along the first array.length.

as far as i understand the variable aux should be pieces of length 5 from the variable l1ca, something like [l1ca[0],..l1ca[4] and then [l1ca[1],...l1ca[5], and so on, but instead am seeing just the last five elements of the main array placed at all over aux variable. am i doing something wrong?.

by the way, am using windows 11 with node v14.15.14 & npm 6.4.10
hope i make myselft explaind and thanks for read!.

[{"id":"934fa1582dce094b","type":"function","z":"e768492ea5ee7729","name":"function 1","func":"var arr_pool = [];\nvar time = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];\nvar l1ca = [46.1, 45.2, 46.6, 47.2, 43.1, 49.3, 52.2, 52.2, 49.1, 50.3];\n\nvar aux = [];\nvar arr_val = [];\n\nfor (let a = 0; a < time.length; a++) {\n    var value = l1ca[a];\n    arr_pool.push(value);\n    arr_val.push(value);\n\n    if (arr_pool.length > 5) {\n        aux.push(arr_pool);\n        arr_pool.shift();\n    }\n}\n\nmsg.payload = [arr_val,arr_pool, aux.length];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":240,"wires":[["978af7dce0fab775"]]},{"id":"82d72ca23e5f9bfd","type":"inject","z":"e768492ea5ee7729","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":240,"wires":[["934fa1582dce094b"]]},{"id":"978af7dce0fab775","type":"debug","z":"e768492ea5ee7729","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":240,"wires":[]}]

To make it easier to debug .. add in your code some node.warn statements
to send a warn msg to the Debug tab and see what value it has with every iteration of the for loop

 if (arr_pool.length > 5) {
        aux.push(arr_pool);
        node.warn({ aux });
        
        arr_pool.shift();
        node.warn({ arr_pool });
    }

it seems like you are pushing to aux the arr_pool array itself instead of its elements ?

Thanks for your reply @UnborN, with those node.warn am seeing what it should looks like :slight_smile:. and yes i'd like to push arr_pool to aux, array to array. And that may be close to my doubt, in my piece of code that is not working properly, it just add the last element, not seeing the data like with node.warn :confused:.

Regards!

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