Having problems iterating over array

What am I doing wrong here that gives me this unexpected result

[{"id":"fef7fe8fd4d77d41","type":"inject","z":"b2bde7ab2cd3fea7","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"2cfd92a831314f8b\",\"cheerlights\"]","payloadType":"json","x":390,"y":220,"wires":[["c729996341d595dd"]]},{"id":"5e372b637dfb3df1","type":"debug","z":"b2bde7ab2cd3fea7","name":"debug 377","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":220,"wires":[]},{"id":"c729996341d595dd","type":"function","z":"b2bde7ab2cd3fea7","name":"function 3","func":"var homemadeSub;\nvar count = 0;\n\nhomemadeSub = (msg['payload']);\nfor (var j in homemadeSub) {\n  node.warn('count:' + count);\n  node.warn('j:' + j);\n  count +=1;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":220,"wires":[["5e372b637dfb3df1"]]}]

FYI I have to use the for (x in array) looping construct as that is what Blocky transcodes to

I think for...in is for key:object.

for arrays, I tend to use incrementing

let homemadeSub;
let count = 0;

homemadeSub = msg['payload'];
for (let i = 0;i<homemadeSub.length;i++) {
  node.warn('count:' + count);
  node.warn('j:' + homemadeSub[i]);

You can also use forEach

homemadeSub.forEach((v,i) =>{
  /* v - Value */
  /* i - Inxex */

Sorry - you missed the extra info I added to my post

Also, this implies that its perfectly valid

Yup the for...in is only returning the index (the key I would guess)

var count = 0;
var homemadeSub = (msg['payload']);
for (var j in homemadeSub) {
  node.warn('count:' + count);
  node.warn('j:' + homemadeSub[j]);
  count +=1;

Yes - that's what I'm expecting

What I'm not expecting is the extra two wierd runs thru the loop :slight_smile:

How odd - I only get one (with 2 warns each)

I too get neither "pairs" nor "forNestedEach".

What does this mean?

var homemadeSub = (msg['payload']);

That's what Blockly transcodes

to JavaScript

It does it very verbosely in order to cope with more complex things

I'll try it out on another instance

Well - it too works fine for me on another instance!

I stopped/started NR on the orig and it still gives weird result :confused:

Nodejs version different for instances?

Does it make a difference if you initialise homemadeSub and/or use let instead of var?

let homemadeSub = []
homemadeSub = (msg['payload']);

No diff

//var homemadeSub;
let homemadeSub = []
var count = 0;

//homemadeSub = ["val1","val2"];
homemadeSub = (msg['payload']);
for (var j in homemadeSub) {
  node.warn('count:' + count);
  node.warn('j:' + j);
  count +=1;

But if I directly set homemadeSub to ["val1","val2"] it works fine

What about let homemadeSub = msg.payload ? (I don't see any reason why it would make a difference, just that Blockly has generated odd looking code)

Makes no diff unfortunately

Going to set up a new @TotallyInformation alternate instance on the same machine and see what happens with it
[edit]Just re-installing node.js first - made no diff

Alternate install works fine - going to re-install main NR now ...

No diff :frowning: still wierd behaviout)

Starting to look like a nuke from orbit solution might be needed :frowning:

So I've re-installed node.js (18.18.2)
re-installed NR

removed node-modules folder and done npm install

Still not working properly :frowning:

for .. in is not intended for use with Arrays. It iterates over all the properties of the Array, including inherited properties, not just the indexed elements. Possibly something you have installed has extended Array.prototype by adding properties.

Use forEach() as suggested.

I wish I could :slight_smile:

So I'm stuck with Blocky transcoding to that syntax and AFAICT it's considered valid JavaScript

And it works for everyone except in this NR instance on this machine

I can work around it by using a different Blocky looping construct but that's just disguising the problem


 // do stuff

But again, that's just hiding the problem

If you temporarily rename your .node-red folder to something else and then restart node-red it will re-create the folder with empty flows and no installed nodes. If you then import your test flow and run it is it ok? If so then it is presumably one of the nodes you have installed, or a library you have imported, causing the problem.
Just be careful when you then delete that folder and rename the original. Possibly make sure your backup of the flows is up to date first.

1 Like

I'm pretty convinced at this point that it's some contrib node I've loaded that's causing this

So I'm trying to find out a different way

Luckliy, this instance only has 20 flows! so I'm copying the flows one at a time across tpo the alternate instance created earlier and then adding in any contrib nodes they need to run.

Doing the flows I care about 1st :slight_smile: