jquery - Javascript some() method and additional parameter - Stack Overflow

I need to check if at least one element in an array pass a condition.The condition depends on a variab

I need to check if at least one element in an array pass a condition. The condition depends on a variable.

For example, I'm using something like this:

function condition(previousValue, index, array) {
    return additonalValue.indexOf(previousValue) + previousValue.indexOf(additonalValue) < -1;
}

I can't figure out how can I pass the "additonalValue" parameter to the array.some(condition) expression.

I'm using jQuery so, an alternative for it is wele.

Is there a way to pass a parameter to array.some() method?

I need to check if at least one element in an array pass a condition. The condition depends on a variable.

For example, I'm using something like this:

function condition(previousValue, index, array) {
    return additonalValue.indexOf(previousValue) + previousValue.indexOf(additonalValue) < -1;
}

I can't figure out how can I pass the "additonalValue" parameter to the array.some(condition) expression.

I'm using jQuery so, an alternative for it is wele.

Is there a way to pass a parameter to array.some() method?

Share edited Jan 8, 2014 at 15:07 Agorreca asked Jan 8, 2014 at 14:57 AgorrecaAgorreca 68617 silver badges31 bronze badges 9
  • 4 Why not pass it through a closure ? – Denys Séguret Commented Jan 8, 2014 at 14:59
  • If you're trying to check if all values in the array match, shouldn't you be using every() ? – adeneo Commented Jan 8, 2014 at 14:59
  • Sorry, I was wrong, I need to check if any element pass a condition. I will modify the question. Thanks :). @dystroy, how would you propose it to do it? – Agorreca Commented Jan 8, 2014 at 15:02
  • Then what do you want to be returned once it passes it? – dcodesmith Commented Jan 8, 2014 at 15:03
  • 1 The fact you name the first argument previousValue is a little disturbing due to the behavior of some. – Denys Séguret Commented Jan 8, 2014 at 15:07
 |  Show 4 more ments

3 Answers 3

Reset to default 3

If you want to pass an additional parameter to the function that is placed inside the some method you can use bind.

var myArray = ['a', 'b', 'c', 'd'];
var otherValue = 'e';
function someFunction(externalParameter, element, index, array) {
    console.log(externalParameter, element, index, array);
    return (element == externalParameter);
}
myArray.some(someFunction.bind(null, otherValue));

This would give you:

e a 0 ["a", "b", "c", "d"]
e b 1 ["a", "b", "c", "d"]
e c 2 ["a", "b", "c", "d"]
e d 3 ["a", "b", "c", "d"]
false

Using a closure looks like the simplest solution :

var additonalValue = 79;
var r = myArray.some(function(previousValue) {
    return additonalValue.indexOf(previousValue) + previousValue.indexOf(additonalValue) < -1;
});

The some() function accepts additional arguments in the form of an array, that is set to this inside the callback, so you could pass a number of values that way :

var arr = ['one', 'two', 'three'];
var val = 'two';

var r = arr.some(function(value) {
    return this[0] == value;
}, [val]);

FIDDLE

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744855961a4597405.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信