Home > Back-end >  How to revise Google Script App to extract multiple arrays and combine them into one var
How to revise Google Script App to extract multiple arrays and combine them into one var

Time:01-21

For this API: https://flowhub.stoplight.io/docs/public-developer-portal/a377fa23cdec5-inventory-items-by-location I'm using this script to extract specific fields from multiple arrays:

function test(e) {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var targetsheet = ss.getSheetByName("test");

var options = {
     //"async": true,
     //"crossDomain": true,
     "method" : "GET",
     "headers" : {
       "clientId" : "1",
       "key": "1",
       "Prefer": "code=200",
       "Prefer":"dynamic=true"
       
       }}




  var text = UrlFetchApp.fetch("https://stoplight.io/mocks/flowhub/public-developer-portal/24055485/v0/locations/1/inventory",options).getContentText();
  var json = JSON.parse(text);


var values = json.data.flatMap(({ productId, cannabinoidInformation, weightTierInformation }) => cannabinoidInformation.map(({lowerRange,name }, i) => [productId, lowerRange, name, weightTierInformation[i].name, weightTierInformation[i].gramAmount]));



targetsheet.getRange(2, 1,values.length, values[0].length).setValues(values);

}

At the moment the script generates the following output:

  • Column A: productId
  • Column B: cannabinoidInformation > lowerRange
  • Column C: cannabinoidInformation > name
  • Column D:weightTierInformation > name
  • Column E: weightTierInformation > gramAmount

I want to revise this code from 1 var:

var values = json.data.flatMap(({ productId, cannabinoidInformation, weightTierInformation }) => cannabinoidInformation.map(({lowerRange,name }, i) => [productId, lowerRange, name, weightTierInformation[i].name, weightTierInformation[i].gramAmount]));

Into 3 var:

  1. var ci = json.data.flatMap(({cannabinoidInformation }) => cannabinoidInformation.map(({lowerRange,name }) => [lowerRange, name]));

  2. var wi = json.data.flatMap(({weightTierInformation }) => weightTierInformation.map(({name,gramAmount }) => [name, gramAmount]));

3. var values - I would like to insert here all the arrays to something like this:productId,ci and wi

That way I can have one final var ("values") which will output all the fields in the order I enter them (I would like to keep the same order A-E as it's outputting now)

CodePudding user response:

From your reply, how about the following modification?

From:

var values = json.data.flatMap(({ productId, cannabinoidInformation, weightTierInformation }) => cannabinoidInformation.map(({lowerRange,name }, i) => [productId, lowerRange, name, weightTierInformation[i].name, weightTierInformation[i].gramAmount]));

To:

var ci = json.data.flatMap(({ cannabinoidInformation }) => cannabinoidInformation.map(({ lowerRange, name }) => [lowerRange, name]));
var wi = json.data.flatMap(({ weightTierInformation }) => weightTierInformation.map(({ name, gramAmount }) => [name, gramAmount]));
var productIds = json.data.flatMap(({ productId, weightTierInformation }) => Array(weightTierInformation.length).fill(productId));
var values = productIds.map((e, i) => [e, ...ci[i], ...wi[i]]);
  • In this modification, in order to retrieve productId, productIds is added. And, the values of ci, wi, productIds are merged.
  • Related