Advertisement
aldikhan13

TIPS WORKING WITH ARRAY IN JAVASCRIPT

Nov 30th, 2020 (edited)
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const arrayOne = [1,2,3,4,5];
  2. const arrayTwo = [6,7,8,9];
  3. const arryObject = [{id: 1, nama: 'restu'}, {id: 2, nama: 'ari'}];
  4.  
  5. const value = 0;
  6.  
  7. // loop data with map i can create new values from old values
  8. const arrMap = arrayOne.map(item =>  item * 2);
  9.  
  10. // loop data with foreach cannot create new values from old value
  11. const arrForeach = arrayOne.forEach(item =>  item * 2);
  12.  
  13. // push new array data with immutabble array
  14. const arrConcat = arrayOne.concat([...arrayTwo]);
  15.  
  16. // alternative - push new array data with immutabble array with spread operator
  17. const spreadOperator = [...arrayOne, ...arrayTwo];
  18.  
  19. // find results values is array iterables
  20. const arrFind = arrayTwo.find(item => item.nama === 'john doe');
  21.  
  22. // filter results values is array literals
  23. const arrFilter = arrayTwo.filter(item => item.nama === 'john doe');
  24.  
  25. // findIndex working with array literals and array iterables
  26. const arrFindIndex = arrayTwo.findIndex(item => item.id === 2);
  27.  
  28. // indexOf working with array literals and not working in array iterables
  29. const arrFindIndexOf = arrayOne.indexOf(4);
  30.  
  31. // some working with array literals and working with array iterables
  32. const arrSome = arryObject .some(item => item.id === 1);
  33.  
  34. // every working with array literals and not working in array iterables
  35. const arrEvery = arrayOne.every(item => item.id === 3);
  36.  
  37. // includes working with array literals and not working in array iterables
  38. const arrIncludes = arrayOne.includes(4);
  39.  
  40. // reduce convert array number literals to single number not for array iterables
  41. const arrReduce = arrayOne.reduce(item => value += item);
  42.  
  43. // delete data with specific value with array literals use with indexOf
  44. // delete data with specific value with array iterables use with findIndex
  45. // suport delete multiple deletes value and pop or shift not support mutiple delete value
  46. const arrSlice = arrayOne.splice(1, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement