简体   繁体   中英

Pushing a Value & Accessing Nested JSON in AngularJS

Im having trouble pushing a value to my nested data inside my Ionic/Angular Project.

I'd like to add a boolean value to each day's time (if it has one) where my json looks like this (I am retrieving the json over a strict http call so its tree structure must not be changed).

{
     "all_year": true,
  "season_from": "01/01",
  "season_to": "12/31",
  "monday": [
    "08:30am"
  ],
  "tuesday": [
    "08:30am"
  ],
  "wednesday": [
    "08:00am", "09:30am", "01:30pm"
  ],
  "thursday": [
    "08:30am", "09:30am"
  ],
  "friday": [
    "08:30am"
  ],
  "saturday": [],
  "sunday": []
}

My result look like this 在此处输入图片说明

my html

 <ion-list ng-repeat="(key, value) in filteredDays"
                    ng-model="value.checked" 
                    ng-checked ="value.checked">
          <div class="item item-divider">
               <h3>{{key}}</h3><!--{{value}}--></div>
          <ion-toggle ng-repeat="x in value"
                             ng-model="value" 
                  ng-checked="x" 
                    >
           {{x}}
             </ion-toggle>

my js

  $scope.filteredDays={};
     $scope.unFilteredDays = {
             "all_year": true,
          "season_from": "01/01",
          "season_to": "12/31",
          "monday": [
            "08:30am"
          ],
          "tuesday": [
            "08:30am"
          ],
          "wednesday": [
            "08:00am", "09:30am", "01:30pm"
          ],
          "thursday": [
            "08:30am", "09:30am"
          ],
          "friday": [
            "08:30am"
          ],
          "saturday": [],
          "sunday": []
      };
    $scope.filteredDays = $scope.unFilteredDays;

//THIS IS WHERE IM STUCK

/*
  var checked = false;
   $scope.filteredDays.forEach($scope.filteredDays, function(value, key) {
   $scope.filteredDays.push(checked);
 });

*/

I left a comment of where I am stuck, I cannot seem to figure out how to push a boolean to the time toggle item array. I setup a codepen here

In addition I've tried working with lodash in this project to handle the data (and get "all_year","season_from","season_to" values out of the list) and believe I have it set up correctly, however, I'm too inexperienced with it to get it to do what I'd like.

Any help would be appreciated.

$scope.filteredDays is not an Array, so doesn't have the forEach method. Instead of this:

var checked = false;
$scope.filteredDays.forEach(function(newCheckItem) {
    newCheckItem.checked = checked;
});

try this:

var checked = false;
Object.keys($scope.filteredDays).forEach(function(key) {
    $scope.filteredDays[key].checked = checked;
});
var dayNames = ["sunday ", "monday",... Etc];
_.forEach($scope.filteredDays, function(v, day) {
  if (_.contains(dayNames, day)) {
    v.push(checked);
  }
});

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