2

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 enter image description here

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.

2
  • Just make an array of day names and check the key name against it. If it's in the day name array then add the Boolean. You can use _.contains or plain old indexOf to check the key Commented Dec 6, 2015 at 1:04
  • Do you think you could make this another answer and show me how I could accomplish that I'm still figuring out lodash @vbranden Commented Dec 6, 2015 at 1:10

2 Answers 2

0

$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;
});
Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't seem to break it but it does seem to push the checked Boolean
0
var dayNames = ["sunday ", "monday",... Etc];
_.forEach($scope.filteredDays, function(v, day) {
  if (_.contains(dayNames, day)) {
    v.push(checked);
  }
});

Comments

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.