Transform array into table

Already did it!

// Code here will run only once, no matter how many input items there are.
// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function

// Loop over inputs and add a new field called 'myNewField' to the JSON of each one
var result = [];
for (item of items) {
  for (data of item.json.data){
    for (values of data.values)
    {
        for (const [key, value] of Object.entries(values.value)) {
          var location = key.split(',')
          result.push({
            "json": {
              "estado": location[1].replace('(state)', '').trim(),
              "cidade": location[0].trim(),
              "total": value
            }
        });
      }
    }
  }
}

result.sort(function(a, b){
  var totalA = a.json.total;
  var totalB = b.json.total;
  if (totalA < totalB) return 1;
  if (totalA > totalB) return -1;
  return 0;
})

// You can write logs to the browser console
console.log('Done!');

return result
6 Likes