Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have my ng-repeat returning arrays like the ones below:

[{"day":"10","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}]
[{"day":"3","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}]

I'd like to create a filter to combine the arrays into one array so that I can use an orderBy | 'day'.

[
{"day":"10","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"},
    {"day":"3","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"
}]

I have a filter used to filter my overall object, but the logic here is much simpler, I'm not sure how to tweak the filter I have to concatenate these objects.

angular.module('hcApp')
.filter('combine', function() {
  return function(items) {
    var temp = [];
    var result = temp.concat.apply(temp,items.map(function(itm){ 
      return temp.concat.apply(temp, Object(itm).map(function(key){ 
       return itm.year[key]; 
  }));
}));  
    return result;
  };
});
share|improve this question

2 Answers 2

up vote 1 down vote accepted

I think since the data comes as array of arrays 2D and to flatten it out, you could just perform a concat on your filter.

angular.module('hcApp')
.filter('combine', function() {
  return function(items) {
     return [].concat.apply([],items)
          .sort(function(a,b){ return +a.day < b.day ? -1 : 1; });//and add sort as well probably
  };
});

Bin

share|improve this answer
1  
Awesome, thanks so much for your help. Not working for some reason, but it seems like it should be. I'll put a Bin together –  byrdr Aug 20 '14 at 4:06
    
@byrdr did it work for u. Saw another question from u. If u have an issue can u post a working plunk il try to help –  PSL Aug 20 '14 at 17:04
    
For some reason I couldn't get the filter to work with my setup even though the resulting array appeared the same as the one in the Bin, which did work with your filter in my view. I'll put a plunkr together –  byrdr Aug 20 '14 at 19:41

I think you can just use .push() to join them together. Since each of your returning arrays consist of 1 element, you will need to access the first element of each data, and the push it to another final array.

  var some_data_1 = [{"day":"10","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}];
  var some_data_2 = [{"day":"3","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}] ;

  var temp = [];
  //push the first element(the only element) in the returning arrays
  temp.push(some_data_1[0], some_data_2[0]);

Here is the working plunkr: http://plnkr.co/edit/t5zRAYmobyYqkEKhrGTd?p=preview

share|improve this answer

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.