0

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

2 Answers 2

2

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/

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

Comments

0

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

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.