Used to filter an array to items for which the closure function returns true.
arrayFilter(array, function(item [,index, array]){} [, parallel] [, maxThreads])
→ returns array
someArray.filter(function(item [,index, array]){} [, parallel] [, maxThreads])
value
*
any
:
The value for the current iteration
index
*
numeric
:
The current index for the iteration
array
*
array
:
A reference of the original array
false
true
false
20
Take an array of struct items and use arrayFilter to return ones of a rating 3 and higher.
fruitArray = [{'fruit'='apple', 'rating'=4}, {'fruit'='banana', 'rating'=1}, {'fruit'='orange', 'rating'=5}, {'fruit'='mango', 'rating'=2}, {'fruit'='kiwi', 'rating'=3}];
favoriteFruits = arrayFilter(fruitArray, function(item){
return item.rating >= 3;
});
writedump(serializeJSON(favoriteFruits));
Expected Result: [{"fruit":"apple","rating":4},{"fruit":"orange","rating":5},{"fruit":"kiwi","rating":3}]
This is the same example as above, but using a member function on the array instead of a standalone function.
fruitArray = [{'fruit'='apple', 'rating'=4}, {'fruit'='banana', 'rating'=1}, {'fruit'='orange', 'rating'=5}, {'fruit'='mango', 'rating'=2}, {'fruit'='kiwi', 'rating'=3}];
favoriteFruits = fruitArray.filter(function(item){
return item.rating >= 3;
});
writedump(serializeJSON(favoriteFruits));
Expected Result: [{"fruit":"apple","rating":4},{"fruit":"orange","rating":5},{"fruit":"kiwi","rating":3}]
Signup for cfbreak
to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.