Advertisement
Nina-S

Untitled

Jan 11th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. let person = {
  2. firstName: "Steven",
  3. lastName: "Queen",
  4. age: 60,
  5. address: {
  6. street: "12 Port str",
  7. city: "Portland",
  8. },
  9. interests: ["writing", "drinking"],
  10. };
  11.  
  12. const getTypeStats = (obj) => {
  13. let newObj = { ...obj };
  14. let resultObj = {
  15. strings: 0,
  16. numbers: 0,
  17. objects: 0,
  18. arrays: 0,
  19. };
  20.  
  21. for (let x in obj) {
  22. if (Array.isArray(x)) {
  23. resultObj.arrays += 1;
  24. }
  25. if (typeof obj[x] === "string") {
  26. resultObj.strings += 1;
  27. }
  28. if (typeof obj[x] === "number") {
  29. resultObj.numbers += 1;
  30. }
  31. if (typeof obj[x] === "object") {
  32. resultObj.objects += 1;
  33. }
  34. }
  35. return newObj;
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement