Change key and value into an object

Hello guys,

I want to export a table as pdf using an external API (PDFGeneratorAPI). The data I want to send is always the same format but not always the same size(Number of date can change according to the billing frequency of my customer).
The problem here is that the final format of my JSON is really nice for what I want to build but I need the key of first object (table3.columns) into Item have the keys equal to their values like this otherwise the api will not be able to loop on the whole of my array :

     "ac_tail": "ac_tail",
      "category": "category",

To build this JSON, I get the data like this (table3is already an array so I concat it into table3.columns previously put between [] to declare it also as an array :

{
   "id" : "111111",
  "name" : "APPENDICES",
  "item" : {{[table3.columns].concat(table3.displayedData)}}
} 

Here is an example of my final JSON where item is an array of object :

{
  "id": "11111",
  "name": "APPENDICES",
  "item": [
    {
      "0": "ac_tail",
      "1": "category",
      "2": "ac_type",
      "3": "2022-01-01",
      "4": "2022-02-01",
      "5": "2022-03-01",
      "6": "2022-04-01",
      "7": "2022-05-01",
      "8": "2022-06-01"
    },
    {
      "ac_tail": "AAAAA",
      "category": "SA",
      "ac_type": "A320",
      "2022-01-01": 30,
      "2022-02-01": 28,
      "2022-03-01": 26,
      "2022-04-01": 28,
      "2022-05-01": 31,
      "2022-06-01": 28
    },
    {
      "ac_tail": "ABBBBB",
      "category": "SA",
      "ac_type": "A320",
      "2022-01-01": null,
      "2022-02-01": 13,
      "2022-03-01": 31,
      "2022-04-01": 16,
      "2022-05-01": 31,
      "2022-06-01": 28
    }
  ]
}

How can I achieve that into on Retool?
Thank you :slight_smile:

Hey @AntoineOA!

Retool comes with lodash built in so you can use the _.mapKeys function here! By their docs this does the following:

The opposite of _.mapValues; this method creates an object with the same values as object and keys generated by running each own enumerable string keyed property of object thru iteratee. The iteratee is invoked with three arguments: (value, key, object).

Since you'd like to map the keys of the table3.columns object to whatever their values are you'd want a function like (value, key, object) => value which can be shortened to value => value... or the _.identity function!

Can you try the following?

1 Like

Hello @Kabirdas,
Your solution worked perfectly. Thank you very much for your help. I really appreciate it!!!