shrskn
shrskn

Reputation: 33

create nested object from array dynamically?

I'm trying to create a list of link on a website using puppeteer. If the webpage has links, go to that link and get a link. I could get the links as an array like myArr, and now I want to format them to the object. I have problem creating nested object from array. Can I create nested object from array like this? Or maybe is there any other good approach?

Expected output:

myObjFromArr = [
   {
      "keyword":"a",
      "children":[
         {
            "keyword":"d",
            "children":[
               {
                  "keyword":"k"
               },
               {
                  "keyword":"l"
               }
            ]
         },
         {
            "keyword":"e",
            "children":[
               {
                  "keyword":"m"
               }
            ]
         },
         {
            "keyword":"f"
         }
      ]
   },
   {
      "keyword":"b",
      "children":[
         {
            "keyword":"g"
         }
      ]
   },
   {
      "keyword":"c",
      "children":[
         {
            "keyword":"h"
         },
         {
            "keyword":"i"
         },
         {
            "keyword":"j"
         }
      ]
   }
  ...
]

What I did:

  1. get link list array using puppeteer.
let count = 0;
let childrenArray = [];

for (let i = 0; i < keywords.length; i++) {
  const keyword = keywords[i];

  if (count < 20) {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    await page.goto(`someurl/${keyword}`);
    count++;

    childArray = await page.$$eval(selector, (list) => {
      return list.map((data) =>
        data.href
      );
    });

    childrenArray[i] = childArray;
  }

  let allKeywords = keywords;
  childrenArray[i] = childrenArray[i].map((item) => {
    allKeywords.push(item);
  });
}

await browser.close();
  1. I've got an array like this:
myArr = [['a', 'b' , 'c'], ['d', 'e', 'f'], ['g'],['h', 'i', 'j'],['k', 'l'],['m']...] 

But not sure how to format this array to create link tree?

Upvotes: 1

Views: 210

Answers (1)

ikhvjs
ikhvjs

Reputation: 5927

You can use a recursive function transform like below.

Each time you call the recursive function you need to calculate the number of previous items and pass it to the recursive again to get the relevant position of the next child array.

data = [
  ["a", "b", "c"],
  ["d", "e", "f"],
  ["g"],
  ["h", "i", "j"],
  ["k", "l"],
  ["m"],
];

const transform = (arr, index) => {
  const store = [];
  if (index < arr.length) {
    let prevItems = 0;

    //get number of previous items
    if (index > 0) {
      for (let j = 0; j < index; j++) {
        for (let k = 0; k < arr[j].length; k++) {
          prevItems += 1;
        }
      }
    }

    for (let i = 0; i < arr[index].length; i++) {
      const obj = transform(arr, i + prevItems + 1);
      if (obj.length) {
        store.push({ keyword: arr[index][i], children: obj });
      } else {
        store.push({ keyword: arr[index][i] });
      }
    }
  }
  return store;
};

const o = transform(data, 0);

console.log(o);

Upvotes: 1

Related Questions