Why does concat give back empty value?

This .concat function does not work.

let device = {"spot1":{"value":
[1,1]
}};
let k = "spot1";
let value = [];
node.warn("value:" + value + ", typeof value:" + typeof value);
if (typeof device[k].value === 'number') {
    node.warn("=number device[k].value:" + device[k].value);
    value.push(device[k].value);
    node.warn("=number value:" + value);
} else {
    node.warn("=else device[k].value:" + device[k].value + ", typeof device[k].value:" + typeof device[k].value);
    value.concat(device[k].value); // <-- this concat does not work
    node.warn("=else value:" + value + ", typeof value:" + typeof value);
}
node.warn("value:" + value + ", typeof value:" + typeof value);
node.warn(device[k].value);
return msg;

example:

[
    {
        "id": "a48e4c1ee433cb0e",
        "type": "function",
        "z": "cbf7f47c.caccc8",
        "name": "",
        "func": "let device = {\"spot1\":{\"value\":\n[1,1]\n}};\nlet k = \"spot1\";\nlet value = [];\nnode.warn(\"value:\" + value + \", typeof value:\" + typeof value);\nif (typeof device[k].value === 'number') {\n    node.warn(\"=number device[k].value:\" + device[k].value);\n    value.push(device[k].value);\n    node.warn(\"=number value:\" + value);\n} else {\n    node.warn(\"=else device[k].value:\" + device[k].value + \", typeof device[k].value:\" + typeof device[k].value);\n    value.concat(device[k].value); // <-- this concat does not work\n    node.warn(\"=else value:\" + value + \", typeof value:\" + typeof value);\n}\nnode.warn(\"value:\" + value + \", typeof value:\" + typeof value);\nnode.warn(device[k].value);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 3670,
        "y": 4720,
        "wires": [
            [
                "e92bb1ca62f3b46b"
            ]
        ]
    },
    {
        "id": "e92bb1ca62f3b46b",
        "type": "debug",
        "z": "cbf7f47c.caccc8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 3860,
        "y": 4720,
        "wires": []
    },
    {
        "id": "71d609dcf5757665",
        "type": "inject",
        "z": "cbf7f47c.caccc8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 3480,
        "y": 4720,
        "wires": [
            [
                "a48e4c1ee433cb0e"
            ]
        ]
    }
]

What is the reason that concat does not add 1,1 to the empty array named "value"?

Because concat returns a new array, does not update the array.

Javascrip Array.Concat

What does that mean?
My base is empty and the injected array is [1,1]
Even with a new Array it should return my injected [1,1] ?
MDN shows that behaviour...

in the else statement which is where your code goes when it runs
value is empty array [] because of let value = [];
and you concat device[k].value which is device['spot1'].value which in turn is [1,1]

so basically with value.concat(device[k].value) you do [].concat([1,1]) and you get [1,1] as expected

I don't get that! Does it on your side?
Is it NR Version related? My Version is 2.1.6


The pointed places are empty.

hmm .. i see .. maybe you need to use JSON.stringify(value)

node.warn("=else value:" + JSON.stringify(value) + ", typeof value:" + typeof value);

No, that does not help.
If I change the debug node to msg.value it is completely "undefined".
And i have the same behaviour with NRv3.1.5

did you assign msg.value to be equal to value before returning the msg ?

btw stringify was just a suggestion to be able to see the value with node.warn

Why have you ignored what @arturv2000 posted.
It clearly shows you need to asssign the concat to a var
e.g.

let new_value = value.concat(device[k].value);

Hello,
As said prieviously by Arthur,
Array1.concat(array2) does not change array1
So if you want to update value you need to update value with the result of value.concat() function
Hope it helps

Pierre

yes .. that was a problem also that i later realized.
well .. It was a combination of issues :wink:

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