Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. if (!Array.prototype.filter){
  2. Array.prototype.filter = function(func, thisArg) {
  3. 'use strict';
  4. if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
  5. throw new TypeError();
  6.  
  7. var len = this.length >>> 0,
  8. res = new Array(len), // preallocate array
  9. t = this, c = 0, i = -1;
  10. if (thisArg === undefined){
  11. while (++i !== len){
  12. // checks to see if the key was set
  13. if (i in this){
  14. if (func(t[i], i, t)){
  15. res[c++] = t[i];
  16. }
  17. }
  18. }
  19. }
  20. else{
  21. while (++i !== len){
  22. // checks to see if the key was set
  23. if (i in this){
  24. if (func.call(thisArg, t[i], i, t)){
  25. res[c++] = t[i];
  26. }
  27. }
  28. }
  29. }
  30.  
  31. res.length = c; // shrink down array to proper size
  32. return res;
  33. };
  34. }
  35.  
  36. Var result = [].filter()
  37.  
  38. // [ 1, 2, 3, 7 ,4] filter even numbers
  39.  
  40. [ 1, 2, 3, 7 ,4].filter(number => number%2);
  41.  
  42.  
  43. Array.prototype.filter = (callbackFunction, array) => {
  44. // callbackFunction.apply(array, this.arguments)
  45. const result = []
  46. Const rsult = array.map((arrayInstance, index) => {
  47. Const booleanResult = callbackFunction.apply(arrayInstance);
  48. Final
  49. result.push();
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement