Advertisement
yarin0600

Untitled

Apr 8th, 2024
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getNestedData(obj, arrOfData = []) {
  2.     if (obj === null) {
  3.         return null;
  4.     } else if (typeof obj === "object") {
  5.         if (Object.keys(obj).length === 1) {
  6.             arrOfData.push(obj);
  7.         }
  8.         for (const key in obj) {
  9.             if (typeof obj[key] === "object") {
  10.                 getNestedData(obj[key], arrOfData);
  11.             } else {
  12.                 arrOfData.push({ [key]: obj[key] });
  13.             }
  14.         }
  15.     } else { // it's not an object or null, it means it's a primitive type.
  16.         arrOfData.push(obj);
  17.     }
  18.     return arrOfData;
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement