Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Lelana villa
Added: Mar 23, 2018 7:47 PM
Views: 16
Tags: no tags
Alongside the function, you can also provide a scope object - a reference to an object which will serve as this inside the function.



every() – returns true if function returns true on every item
filter() – returns an array of all items for which the function returns true
forEach() – no return value (just run the function on every element in the list)
map() – returns a new list with the result of each item in an array
some() – returns true if the function returns true for at least one of the items
  1. var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  2. var every_result_is_smaller_than_9 = numbers.every(item => item < 9);
  3.  
  4. // this is equivalent with
  5. var every_result_is_smaller_than_9 = numbers.every(function(item) { return item < 9; });
  6.  
  7.  
  8. console.log(every_result_is_smaller_than_9);  // false
  9.  
  10. var some_resulst_are_smaller_than_9 = numbers.some(item => item < 9);
  11. console.log(some_resulst_are_smaller_than_9); // true
  12.  
  13. var odd_numbers = numbers.filter(item => item % 2 == 1);
  14. console.log(odd_numbers);        // [1, 3, 5, 7, 9]
  15.  
  16. numbers.forEach(item => console.log("works"));
  17.  
  18.  
  19. var numbers_plus_one = numbers.map(item => item + 1);
  20. console.log(numbers_plus_one);