0

I have this json response:

 {
  "tags": [
    {
      "name": "SolarData",
      "results": [
        {
          "groups": [
            {
              "name": "type",
              "type": "number"
            }
          ],
          "attributes": {
            "customer": [
              "Acme"
            ],
            "host": [
              "server1"
            ]
          },
          "values": [
            [
              1429950000000,
              46,
              3
            ],
            [
              1429960000000,
              62,
              3
            ],
            [
              1429970000000,
              183,
              3
            ],
            [
              1429980000000,
              156,
              3
            ],
            [
              1429990000000,
              205,
              3
            ]
          ]
        }
      ],
      "stats": {
        "rawCount": 5
      }
    }
  ]
}

and I want to be able to only get the first two items of every value part of the item. Foe example I want to return [[1429950000000,46],[1429960000000,62],[1429970000000,183],.....] in a scope variable so I can eventually use it for a graph. I am new to angular and web dev in general but this is the way I've tried it so far.

$http({
           url: 'file.json',
           method: 'POST',    
           data: '(query data here)'
         }).then(function(data, status){
            $scope.solarData = data.tags.results.values;
            conosle.log($scope.solarData);
        });

3 Answers 3

2

You can use map:

var custom = data.tags[0].results[0].values.map(function(values) {
  return [values[0], values[1]];
});

You can use slice if you want to return a lot of items or a variable number of them like

return values.slice(0, 2);
//---------------------^ replace this

var data = {
  "tags": [{
    "name": "SolarData",
    "results": [{
      "groups": [{
        "name": "type",
        "type": "number"
      }],
      "attributes": {
        "customer": [
          "Acme"
        ],
        "host": [
          "server1"
        ]
      },
      "values": [
        [
          1429950000000,
          46,
          3
        ],
        [
          1429960000000,
          62,
          3
        ],
        [
          1429970000000,
          183,
          3
        ],
        [
          1429980000000,
          156,
          3
        ],
        [
          1429990000000,
          205,
          3
        ]
      ]
    }],
    "stats": {
      "rawCount": 5
    }
  }]
}

var custom = data.tags[0].results[0].values.map(function(values) {
  return [values[0], values[1]];
});
console.log(custom);

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

5 Comments

Nice'n clean. Though I wonder if return [values[0], values[1]] could be faster. (The difference is probably negligible, any way)
@Cerbrus that's correct it'll be faster since it avoids a function invocation.
Tried this method and I keep getting a "Cannot read property '0' of undefined" at tags[0] error.
@KrustyCheerio It works with the same data you posted... maybe you're getting different data in which there is no tags array?
Changed it to data.data.tags[0].results[0].values.map.... And it worked perfect! Thanks your your help.
0
var requirementArray = new Array();
for (i = 0; i < $scope.solarData.length ; i++) {
    var pare = new Array();
    pare.push($scope.solarData[i][0]);
    pare.push($scope.solarData[i][1]);

    requirementArray.push(pare);
}

requirementArray will be :

[[1429950000000,46],[1429960000000,62],[1429970000000,183],.....]

Comments

0

You can use Array.map for that:

$http({
   url: 'file.json',
   method: 'POST',    
   data: '(query data here)'
 }).then(function(data, status){
    $scope.solarData = data.tags[0].results[0].values.map(
        function(curVal, index, arr) {
            return [curVal[0], curVal[1]];
        }
    );

    conosle.log($scope.solarData);
});

5 Comments

Why did you add the index, arr and status parameters if you don't use them?
@Cerbrus For the clarity of the example, in case he doesn't read the documentation about the function, now op still knows the parameters exist
And how would he know what status means without reading the docs? index and arr are a little more self-explanatory, but you'd be better off linking to documentation.
@Cerbrus I just copied that from his own example
Ah, didn't check that.

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.