0

I'm new to AngularJs and got stuck with pretty basic problem.

I'd simply like to store particular value(s) from my JSON file where key is: "timestamp_list_for_days_for_table_7x6" and pass it through $scope.

  {
    "month_year": "2015",
    "month_number": "11",
    "month_slo_name": "November",
    "st": "30",
    "timestamp_list_for_days_for_table_7x6": "0;0;0;0;0;0;1446375600;1446462000;1446548400;1446634800;1446721200;1446807600;1446894000;1446980400;1447066800;1447153200;1447239600;1447326000;1447412400;1447498800;1447585200;1447671600;1447758000;1447844400;1447930800;1448017200;1448103600;1448190000;1448276400;1448362800;1448449200;1448535600;1448622000;1448708400;1448794800;1448881200;0;0;0;0;0;0"
  },
  {
    "month_year": "2015",
    "month_number": "12",
    "month_slo_name": "December",
    "st": "31",
    "timestamp_list_for_days_for_table_7x6": "0;1448967600;1449054000;1449140400;1449226800;1449313200;1449399600;1449486000;1449572400;1449658800;1449745200;1449831600;1449918000;1450004400;1450090800;1450177200;1450263600;1450350000;1450436400;1450522800;1450609200;1450695600;1450782000;1450868400;1450954800;1451041200;1451127600;1451214000;1451300400;1451386800;1451473200;1451559600;0;0;0;0;0;0;0;0;0;0"
  }

This is my angularJs controller code. Here I'd like to store $scope.timestamps = ???; and later I need to split values in string of each key:"timestamp_list_for_days_for_table_7x6" so that I can iterate in html with 'ng-repeat'

.controller('DataCtrl', function($scope, $http) {

$http.get("/www/js/data/data.json")
    .then(function(results){
        //Success
        $scope.data = results.data;        

    }, function(results){
        //error
        console.log("Error: " + results.data + "; "+ results.status);
    });

})

Any suggestions? Tnx

3
  • What do you mean by "Here I'd like to store $scope.timestamps = ???"? An array with all the values of result.data with the key timestamp_list_for_days_for_table_7x6? Commented May 4, 2014 at 12:24
  • $scope.data = results.data; here I get all JSON data. I would like to get only timestamp_list_for_days_for_table_7x6 values, not all of them (month_year, month_number, ...) Commented May 4, 2014 at 12:29
  • I need to store timestamp_list_for_days_for_table_7x6 somehow so that I can split string by ',' (and pass it thru $scope) Commented May 4, 2014 at 12:34

1 Answer 1

1

It looks like you want to map the collection of results to an array of timestamps. jQuery or Underscore have a mapping function, but IE9+ can take advantage of Array.prototype.map(). You could map to your timestamps like:

$scope.timestamps = data.map(function(item) {
   return item.timestamp_list_for_days_for_table_7x6;
});

If you wanted to go ahead and split the data, you could! Like:

$scope.timestamps = data.map(function(item) {
   return item.timestamp_list_for_days_for_table_7x6.split(';');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you can use jQuery's $.map or better yet, use a polyfill for Array.prototype.map, such as es5-shim.

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.