0

I have a code written that returns a json as below.

"aggrRes": {
"totalSize": 3,
"done": true,
"records": [
  {
    "attributes": {
      "type": "AggregateResult"
    },
    "expr0": "2019-02-25",
    "expr1": 40,
    "expr2": 27
  },
  {
    "attributes": {
      "type": "AggregateResult"
    },
    "expr0": "2019-02-22",
    "expr1": 36,
    "expr2": 9
  },
  {
    "attributes": {
      "type": "AggregateResult"
    },
    "expr0": "2019-02-21",
    "expr1": 38,
    "expr2": 12.5
  }
 ]
}

Using this json I want to create another json. and here the format should be something like below.

in forloop:
   dataset:
      dates:[get all expr0 for all 3 records],
      expr1:[get all expr1 for all 3 records],
      expr2:[get all expr2 for all 3 records]

Here the main part that I'm confused is on how can I do the below.

var agResp = this.total_info.aggrRes;
for(int i=0; i<records.size; i++){
console.log(agResp.records[0].expr**i**);
}

Here I want to get the expr value by looping on the i value, that will generate me expr0, expr1 and expr2 values.

Thanks

2
  • you mean your expr0, expr1 is dynamic? it can be upto n values? like expr11.....or is it limited to expr2 only? Commented Feb 25, 2019 at 6:26
  • thanks for the quick reply @UbiquitousDevelopers, currently it is limited to expr0, expr1, expr2, but it might go dynamic
    – Rakesh K
    Commented Feb 25, 2019 at 6:29

1 Answer 1

1
var records = aggrRes.records; // Store Records in a variable
var values = [];

records.forEach(record => {
  Object.keys(record).forEach(key => {
    if (values[key]) {
      values[key].push(record[key]);
    } else {
      values[key] = [record[key]];
    }
  });
});
console.log(values);

Will return something like this

attributes: (3) [{…}, {…}, {…}]
expr0: (3) ["2019-02-25", "2019-02-22", "2019-02-21"]
expr1: (3) [40, 36, 38]
expr2: (3) [27, 9, 12.5]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.