Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a array of objects. the object contains:
code
name.

example:

var fullList = [
      {"code":10,"name":"example 10"},
      {"code":50,"name":"example 50"},
      {"code":60,"name":"example 60"}
   ]

I have another array with only code, like:

var filterBy = [10, 50]

I want to create 2 new array:
First - contains only objects that their code in "filterBy"
Second - contains only objects that their code not (!) in "filterBy"

how can i do it?

Thank you

share|improve this question
up vote 1 down vote accepted

Just pass in a function as the filter expression

<div ng-repeat="a in fullList | filter: myFilterBy">
  {{ a }}
</div>

and then

$scope.myFilterBy = function(e) {
  return filterBy.indexOf(e.code) !== -1;
}

Fiddle - https://jsfiddle.net/7tda0bbL/

share|improve this answer
    
wow! perfect and elegant solution...! Thank-you! – prog_prog Mar 27 '16 at 12:45

You can create 2 arrays as follows -

var arr1 = [];
var arr2 = [];
angular.forEach(fullList, function(obj, key) {
    var indx = filterArray(obj);
    if (indx > -1) {
      arr1.push(obj);
    } else {
      arr2.push(obj);
    }
});
console.log(arr1);
console.log(arr2);

function filterArray(arrObj) {
    return [10, 50].indexOf(arrObj.code);
};
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.