在函数内返回0到n个数组值 [英] Return 0 to n arrays values inside a function
问题描述
我有这个动态数组,可能包含0到 - > n个值:
这是一个例子:
var array = [2,3,8];
这是第二个例子:
var array = [2,3,8,15,25];
现在,我需要执行这个函数(由stakoverflower给出),但是我不知道如何每次考虑每个值;这是一个静态函数,现在,这是行之有效的:
$ $ $ $ $ $ $ c $ var filterList $ filter('filter')($ scope .footballers,function(i){
return(i.identifiant === 2 || i.identifiant === 3 || i.identifiant === 8);
});
但是看起来,它适用于第一个数组示例,但不适用于第二个数组示例。如果我想对待第二个数组,我需要输入:
<$ c
c> var filteredList = $ filter('filter')($ scope.footballers,function(i){
return(i.identifiant === 2 || i.identifiant === 3 || i.identifiant === 8 || i.identifiant === 15 || i.identifiant === 25);
});
我的问题是:如何在$ filter函数中添加for循环,对待第二个数组或任何长的数组?
如果你有一个想法,非常感谢你。然后循环会自动输入i.identifiant === array [0],i.identifiant === array [1]等等!
我从来没有在这种情况下,我真的不知道如何返回一个动态的长数组!
这也适用于第一个例子:
var filteredList = $ filter('filter')($ scope.footballers,function(i){
return(i.identifiant === array [0] || i.identifiant === array [1] || i.identifiant === array [2]);
});
但是我怎么能如果我的数组包含12个值?我需要一个循环,但我不知道该怎么做!
然后循环会输入i.identifiant === array [x]
哎呀,我忘记了,我的目标是显示一个足球运动员名单列表中的阵列阵列内使用angularjs。进入拖放应用程序。这就是为什么我过滤,$ scope.footballers内有大量的标识符,但我只想保留一些,这些在阵列内呈现!问题是数组数组可能包含2个值以及25个值。
我正在考虑一个内部或末尾的eval()循环
好的JB Nizet给了我答案,非常感谢,它超过了我的水平,但它工作,所以好的!
如果你对我的实验室感到好奇,那就在这里:
http://nicolash.org/football/#/liste
有一个愉快的一天
$ b $是可能过滤角度js的由包含另一个数组b
最后得到了这个反函数:
$ b $ $ $ $ $ $ $ $ $ $ $ $ scope $ footer $ filter $' )($ scope.footballers,函数(i){
return presence_ equipe.indexOf(i.identifiant)=== -1
});
非常感谢!
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?
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 !!
这篇关于在函数内返回0到n个数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!