1

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 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
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
0

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

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.