serdarshefketov

if

Jan 12th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // getTypeStats
  2.  
  3. const getTypeStats = (obj) => {
  4.  
  5. let newObj = {};
  6.  
  7.  
  8.  
  9. let keys = Object.values(obj);
  10.  
  11. let strings = 0;
  12.  
  13. let numbers = 0;
  14.  
  15. let objects = 0;
  16.  
  17. let arrays = 0;
  18.  
  19.  
  20. for (const key of keys) {
  21.  
  22. if((typeof key) === 'string') {
  23.  
  24. strings++;
  25.  
  26. newObj['strings'] = strings;
  27.  
  28.  
  29. } else if ((typeof key) === 'number') {
  30.  
  31. numbers++;
  32.  
  33. newObj['numbers'] = numbers;
  34.  
  35.  
  36. } else if ((typeof key) === 'object' && key !== null && !Array.isArray(key)) {
  37.  
  38. objects++;
  39.  
  40. newObj['objects'] = objects;
  41.  
  42.  
  43.  
  44. } else if ((Array.isArray(key))){
  45.  
  46. arrays++;
  47.  
  48. newObj['arrays'] = arrays;
  49.  
  50. }
  51.  
  52. }
  53.  
  54. return newObj;
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment