Linked Questions

Popular Questions

I just want to create a nested array of this format:

const myArray = [
  {
   id: 1,
   name: "foo",
   children: [
      {id: 1-1, name: "foo", children: []}, //so on and so forth
      //...
   ]
  },
  //...
]

Each nested children would have to adopt the id of its parent and attach its own ${parentId}-${childrenId}

Now I have to base this on a specific length and create a random nested array with a total collective items of whatever length I specify.

const length = 50;

And I have a method that returns a base object which generates random names and accepts an id like so:

const createObject = function (id) {
    return {
        Id: id,
        Name: Math.random().toString(36).substr(2, 5),
    }
}

What I mean about "random nested array of objects" is that each item could have an arbitrary amount of children, or no children at all. This nested array would have a total items of x when counted recursively. The depth is not the same for each series. It could have a max depth of 8 if that's easier to make.

I literally have no idea how to start making such random data generator. I could randomize a range from my count while checking if the total accumulated items exceeds then do a recursive and such, but I feel like it is too time-consuming and am wondering if there are better approaches.

Thanks for the help!

Related Questions