How to rebuild array

original

[
    {
        "deviceuid": "704F08017964",
        "zoneNum": 0,
        "historyMusic": {
            "isLocal": false,
            "localPath": "",
            "singerId": "",
            "musicId": "69934500505"
        }
    },
    {
        "deviceuid": "704F08017964",
        "zoneNum": 0,
        "historyMusic": {
            "isLocal": false,
            "localPath": "",
            "singerId": "",
            "musicId": "69010400010"
        }
    }
]

rebuild

{
    "appkey": "wisemobile02",
    "zoneNum": "0",
    "musicIndex": 0,
    "deviceuid": "704F08018888",
    "userAccount": "18012971010",
    "musicInfo": [
        {
            "isLocal": false,
            "localPath": "",
            "singerId": "",
            "musicId": "69934500505"
        },
        {
            "isLocal": false,
            "localPath": "",
            "singerId": "",
            "musicId": "69010400010"
        }
    ]
}

With no description i would guess

[{"id":"c828b9168a3f213f","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"deviceuid\":\"704F08017964\",\"zoneNum\":0,\"historyMusic\":{\"isLocal\":false,\"localPath\":\"\",\"singerId\":\"\",\"musicId\":\"69934500505\"}},{\"deviceuid\":\"704F08017964\",\"zoneNum\":0,\"historyMusic\":{\"isLocal\":false,\"localPath\":\"\",\"singerId\":\"\",\"musicId\":\"69010400010\"}}]","payloadType":"json","x":250,"y":3100,"wires":[["1fa7f070dcc73066"]]},{"id":"1fa7f070dcc73066","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload{\t       \"appkey\": \"wisemobile02\",\t       \"zoneNum\": $distinct(zoneNum),\t       \"musicIndex\": 0,\t       \"deviceuid\": $distinct(deviceuid),\t       \"userAccount\": \"18012971010\",\t       \"musicInfo\": historyMusic[]\t    }\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":3080,"wires":[["a81d87f562b74deb"]]},{"id":"a81d87f562b74deb","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 225","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":3120,"wires":[]}]

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

tks , i edit my post , code looks correct now

var array = [""];// list of names
var hold_obj = {};// `temp object
array.forEach((v, i) => hold_obj[v] = msg.payload.data[i].historyMusic) //loop through names and add to object


msg.payload = hold_obj; // move back to payload
var musicinfo = hold_obj

msg.payload = {
    "appkey": "wisemobile02", "zoneNum": "0", "musicIndex": 0, "deviceuid": "704F08018888", "userAccount": "18012971010", musicinfo
}
return msg;

I tried to write the rebuild code, but the array can only display the first one, and the latter ones disappear

msg.payload = hold_obj will be overwritten by the second msg.payload and musicinfo is only a reference to it (not an actual new object/array)

ic. how to create array ?

Lots of ways to merge arrays. Have a search for "javascript merge arrays".

Or, if you were adding 1 entry at a time, you would use sometihng like:

msg.payload = []
msg.payload.push('another-one')

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