2

I have an object that I get from a URL request:

var data = {
    "DataSet": {
      "Series": [
     {
      "@FREQ": "A",
      "@REF_AREA": "BF",
      "@INDICATOR": "NGDP_R_PC_PP_PT",
      "@UNIT_MULT": "0",
      "@TIME_FORMAT": "P1Y",
      "Obs": [
        {
          "@TIME_PERIOD": "2018",
          "@OBS_VALUE": "6"
        },
        {
          "@TIME_PERIOD": "2019",
          "@OBS_VALUE": "5"
        },
        {
          "@TIME_PERIOD": "2020",
          "@OBS_VALUE": "1"
        },
        {
          "@TIME_PERIOD": "2021",
          "@OBS_VALUE": "5"
        }
      ]
    },
    {
      "@FREQ": "A",
      "@REF_AREA": "CI",
      "@INDICATOR": "NGDP_R_PC_PP_PT",
      "@UNIT_MULT": "0",
      "@TIME_FORMAT": "P1Y",
      "Obs": [
        {
          "@TIME_PERIOD": "2018",
          "@OBS_VALUE": "6"
        },
        {
          "@TIME_PERIOD": "2019",
          "@OBS_VALUE": "6"
        },
        {
          "@TIME_PERIOD": "2020",
          "@OBS_VALUE": "2"
        },
        {
          "@TIME_PERIOD": "2021",
          "@OBS_VALUE": "8"
        }
      ]
    }
  ]
}

} And I want an array that looks like this, each of the years and values from each 'Obs' array in its own array:

var dataSet = [
[["2018","6"],["2019","5"],["2020","1"],["2021","5"]],
[["2018","6"],["2019","6"],["2020","2"],["2021","8"]]
]

I have tried this:

let dataO = []  
var dataSet = data.Series.map((x,index) => {                  
  dataO.push(x.Obs.map(i=>i['@TIME_PERIOD']))
  dataO.push(x.Obs.map(r=>r['@OBS_VALUE']))
  return dataO
})

But I get an array with 2 elements, each containing all of the data from the object. I can't see where I am going wrong. Any help is appreciated. Thanks!

2 Answers 2

1

Simple double loop:

var dataSet = [];
data.DataSet.Series.map((x) => {
      var periods = [];
      x.Obs.map((y) => {
          periods.push([y['@TIME_PERIOD'], y['@OBS_VALUE']]);
      }); 

      dataSet.push(periods);
});

Testable below:

var data = {
  "DataSet": {
    "Series": [{
        "@FREQ": "A",
        "@REF_AREA": "BF",
        "@INDICATOR": "NGDP_R_PC_PP_PT",
        "@UNIT_MULT": "0",
        "@TIME_FORMAT": "P1Y",
        "Obs": [{
            "@TIME_PERIOD": "2018",
            "@OBS_VALUE": "6"
          },
          {
            "@TIME_PERIOD": "2019",
            "@OBS_VALUE": "5"
          },
          {
            "@TIME_PERIOD": "2020",
            "@OBS_VALUE": "1"
          },
          {
            "@TIME_PERIOD": "2021",
            "@OBS_VALUE": "5"
          }
        ]
      },
      {
        "@FREQ": "A",
        "@REF_AREA": "CI",
        "@INDICATOR": "NGDP_R_PC_PP_PT",
        "@UNIT_MULT": "0",
        "@TIME_FORMAT": "P1Y",
        "Obs": [{
            "@TIME_PERIOD": "2018",
            "@OBS_VALUE": "6"
          },
          {
            "@TIME_PERIOD": "2019",
            "@OBS_VALUE": "6"
          },
          {
            "@TIME_PERIOD": "2020",
            "@OBS_VALUE": "2"
          },
          {
            "@TIME_PERIOD": "2021",
            "@OBS_VALUE": "8"
          }
        ]
      }
    ]
  }
};

var dataSet = [];
data.DataSet.Series.map((x) => {
      var periods = [];
      x.Obs.map((y) => {
          periods.push([y['@TIME_PERIOD'], y['@OBS_VALUE']]);
      }); 

      dataSet.push(periods);
});

console.log(dataSet);

Sign up to request clarification or add additional context in comments.

Comments

1

You can use flatMap:

data.DataSet.Series
    .flatMap(x => x.Obs)
    .map(x => [x["@TIME_PERIOD"], x["@OBS_VALUE"]])

1 Comment

Thanks but this gave one array with all the values in. Although great to know about flatMap(), I can make use of that a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.