I have a complex array which has objects inside of parent objects. I've already extracted the objects I want to work with through ng-repeat and am returning them. I'd like to be able to put all of these objects into an array using an angular forEach. I'm not quite sure how to go about doing it.

Here are the objects I'm returning:

{"day":"21","title":"ok","summary":"ok","description":"ok","_id":"53ee9f0fc6aed109c6d33cfd"}
{"day":"2","title":"ok","summary":"ok","description":"ok","_id":"53ee9f038782309c6d892"}
{"day":"27","title":"ok","summary":"ok","description":"ok","_id":"533240fc6ae32433chd"}

Here is my view: The objects above are represented by 'items' in the ng-repeat.

    <div class="calDynamic" data-ng-repeat="n in [] | range:100">
<div ng-repeat="cal in calendar[n].year | filterKey:month">
  <div ng-if="cal != '' ">
    <div class="calendar">

    <p>{{cal}}</p>
<div ng-repeat="items in cal ">

        <a href="/events/{{items.day}}">
          <article class="eventslist">
           <div class="numberedDate">
               <h3>{{items.day}}</h3>
            </div>
            <div class="calInfo">
            <h5>{{items.title}}</h5>
               <p>{{items.summary}}&nbsp;<a>more</a></p>
            </div>
           </article>


      </div><!-- ng-repeat val,key -->
</div><!-- calendar -->
</div><!-- ng-if cal -->
</div><!-- ng-repeat cal -->
</div><!-- calDynamic -->

How would I go about using a foreach loop like the one below to store those returned objects into an array so that I could use the filter method on it?

var arr = [];
angular.forEach(items, function (item) {
    arr.push(item);
});
share
    
No, I'd like to sort the array by the 'day' value. If possible I'd like to create the array from the objects and then use that data in the view. The overall goal is to be able to use a filter on the data. – byrdr Aug 19 '14 at 16:57
    
In that case just filter on the ng-repeat="items in cal | insertdayfilterhere" – Malkus Aug 19 '14 at 16:58
    
You can also have more than one filter on a filter just use | to separate them if you wanted to add to the parent ng-repeat "cal in calendar[n].year | filterKey:month | insertdayfilterhere"> – Malkus Aug 19 '14 at 16:59
    
I've tried those, it seems like the complexity of the object is preventing the filtering of the day value. Here's the entire object which the 'items' object is being pulled from. – byrdr Aug 19 '14 at 17:04
    
{ "_id" : ObjectId("5"), "year" : { "December" : [], "November" : [], "October" : [], "September" : [], "August" : [], "July" : [ { "day" : "21", "title" : "u", "summary" : "u", "description" : "ok", "_id" : ObjectId("5366d") } ], "June" : [], "May" : [], "April" : [], "March" : [], "February" : [], "January" : [] }, } – byrdr Aug 19 '14 at 17:04

If you have the list already available in the controller you can just do a deep copy.

    var arr = [];
    angular.copy(items,arr);

If you do not use angular.copy() and just try to use arr = items; angular will just create a pointer, which you do not want in this case


angular.copy reference

share
    
I see, so 'items' in the controller will reference the variable items which is set in the view? – byrdr Aug 19 '14 at 16:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.