Best way to wrangle json data to remove field labels

Viewed 47

I am looking to try make it easier to use the Google Analytics data in Google sheets.

The outputs that I receive from the api look like :

    [
  {
    "dimensionValues": [
      {
        "value": "id2"
      },
      {
        "value": "(not set)"
      },
      {
        "value": "Android"
      }
    ]
  },
  {
    "dimensionValues": [
      {
        "value": "id1"
      },
      {
        "value": "stream name"
      },
      {
        "value": "iOS"
      }
    ]
  }
]

Whats the most efficient method to remove the field names to make a "flatter set of arrays" similar to:

[["id2","(not set)","Android"], ["id1","stream name","iOS"]]

It feels like there should be a quick way to do this!

1 Answers

You can use a double map for this one. Map the value to its dimensionValues and then map those to the array elements to get the designated output.

let array = [{"dimensionValues": [{"value": "id2"},{"value": "(not set)"},{"value": "Android"}]},{"dimensionValues": [{"value": "id1"},{"value": "stream name"},{"value": "iOS"}]}]  

console.log(array.map(x => x.dimensionValues.map(y => y.value)))

Related