How can I create a functions within JavaScript, that i can then use and chain with others to get results from an array.
I have tried creating a class with multiple methods that can be chain together but that wont allow me to call the functions directly on the array itself.
Example:
[23,45,87,89,21,234,1,2,6,7].CustomFuncEvenNumbers().CustomFuncOrderNumbers()
You can create the custom function using prototype but to chain another function you need to explicitly return from your custom function , else it will return undefined
let arr = [23, 45, 87, 89, 21, 234, 1, 2, 6, 7];
Array.prototype.CustomFuncEvenNumbers = function() {
return this.filter(function(item) {
return item % 2 === 0
})
}
let k = arr.CustomFuncEvenNumbers().map(item => item * 2);
console.log(k)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With