Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

angularjs - Return 0 to n arrays values inside a function

I've got a really complex problem.

I've got this dynamic array , that might include 0 to -> n values :

This is an example :

var array =[2,3,8];

this is the second example :

var array =[2,3,8,15,25];

Now, i need to execute this function( gently given by a stakoverflower), but i don't know how to consider each values each times; it is a static function for now, that is working well :

var filteredList = $filter('filter')($scope.footballers, function (i) {
      return (i.identifiant === 2 || i.identifiant === 3 || i.identifiant === 8);
 });

But look, it works for the first array example, but not for the second array example.

If i want to treat the second array, i need to type this :

 var filteredList = $filter('filter')($scope.footballers, function (i) {
      return (i.identifiant === 2 || i.identifiant === 3 || i.identifiant === 8 || i.identifiant === 15 || i.identifiant === 25);
 });

My question is : How could i add a for loop inside the $filter function, to be able to treat the second array or whatever long the array is ?

Thank you a lot if you have an idea. The loop would then automatically type i.identifiant === array[0], i.identifiant === array[1] and so on !

I've never encoutered this case, and i really don't know how to return a dynamic long array !

This would also work for the first example :

    var filteredList = $filter('filter')($scope.footballers, function (i) {
      return (i.identifiant === array[0] || i.identifiant === array[1] || i.identifiant === array[2]);
 });

But how could i do if my array contains 12 values ??? I need a loop, but i dont know how to do at all !

The loop would then be supposed to type "i.identifiant === array[x]"

Ooops , i forgot, my goal is to display a list of footballers whoses identifiers are presents inside the array "array", using angularjs. Into a drag and drop application. That is why i filter, there are a large amount of identifiers inside $scope.footballers, but i only wanna keep a few, the ones presents inside the array ! The problem is that the array "array" may contain 2 values as well as 25 values .

I was thinking about a loop with an eval() inside or at the end?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok JB Nizet gave me the answer, thank you a lot, it's above my level, but it is working, so nice !.

If u're curious about my labo, it's here :

http://nicolash.org/football/#/liste

have a nice day

Solved the inverse problem with this : Is it possible to filter angular.js by containment in another array?

And finally got that inverse function :

$scope.footballers = $filter('filter')($scope.footballers, function (i) {
       return presence_equipe.indexOf(i.identifiant) === -1
});

thanks a lot !!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...