How can I change the order of values within each element of an array?

Hey!
How can I change the order of values within each element of an array?

State A:

"Оплата":
[
	{
	"ПорядковыйНомер": 1,
	"ВариантОплаты": "Кредит (плановый)",
	"ДатаПлатежа": "20210409",
	"СуммаПлатежа": 50000
	}
]

State B:

"Оплата": [
{
"ПорядковыйНомер": 1,
"СуммаПлатежа": 100000,
"ВариантОплаты": "Кредит (плановый)",
"ДатаПлатежа": "20210409"
}
]

One option:

return [].concat(
	...items.map(i => i.json.Оплата.map(({ ПорядковыйНомер, СуммаПлатежа, ВариантОплаты, ДатаПлатежа }) => {
		return { json: { ПорядковыйНомер, ВариантОплаты, ДатаПлатежа, СуммаПлатежа } };
	}))
);
1 Like

Hey @Roket!

You can also use the Set node to do this, if you don’t want use the Function node. Below is an example:

{
  "nodes": [
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "data.one",
              "value": "={{$json[\"data\"][\"one\"]}}"
            },
            {
              "name": "data.three",
              "value": "={{$json[\"data\"][\"three\"]}}"
            },
            {
              "name": "data.two",
              "value": "={{$json[\"data\"][\"two\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Arrange items",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "data.one",
              "value": "item 1"
            },
            {
              "name": "data.two",
              "value": "item 3"
            },
            {
              "name": "data.three",
              "value": "item 2"
            }
          ]
        },
        "options": {}
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Arrange items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

harshil1712, thanks for your attention to my problem, but this does not work in this case, i.e. in a set node it is possible to swap places only within a type (number or string or boolean).

Sorry for the quick conclusions about your solution, everything works even if you use the string type for the input data number.

1 Like

No issues! I am glad that it works for you!
Have fun :tada: