简体   繁体   中英

AngularJs get particular key value from JSON and store it to $scope

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

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(';');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM