0

I'm trying to solve a problem with a Node API I'm working on and I got stuck at something that should be easy but I just can't figure it out. Let's say I have an array like this:

[1, 2, 1, ....]

This array can be arbitrarily long. I need to use the values in the array as keys for an object, so for the example above, I need to be able to do something like anotherObject[1][2][1] = 'hello world' no matter how long the array is.

I've already figured out how to get the value of the object using a while loop or a recursive function but I can't figure out how to set the value like that.

(Just in case anyone figures out a way to avoid this problem all together and do what I'm trying to do differently, I'm gonna briefly sum up what I'm working on.

Imagine a social network like Twitter where you can write a post as a reply to another post. Then someone can reply to your post and so on so there can be a huge stack of posts each replying to the one that came before it. But there can also be a bunch of replies that are on the same level - all replying to the same post but not to each other. The way I store this in the database is pretty simple: if a post is a reply, it has a property that refers to the ID of the post it's replying to. If you can figure out a better way to store it, I'd love to hear it.

Now here's the issue. How do you display all the replies to a certain post and also all the replies to those replies and all the replies to the replies to those replies and so on? I've come up with a pseudo-algorithm that creates something resembling a DOM made up of comments with children and siblings and so on. I just need a way to navigate around it and modify the it. So logically, I need a way to keep track of what is what and a way to modify the individual elements. So I have the array keeping track of what the algorithm is processing at the moment and then the object that keeps track of which posts have been discovered and which ones have been processed already. If there's a better way to solve this issue, I'd love to hear it)

M. Farnik
  • 235
  • 1
  • 3
  • 9
  • in reality, you have a single data set with an id and a parent and other information of a post. this is not nested in the data base and the rendering in a webpage is driven by date and where parent is `null` or any other indicator for parent entries. actually i don't unterstand, what the question is about, the storage or the rendering? – Nina Scholz Aug 16 '18 at 18:45
  • The structure you are describing is not called "a DOM" but simply a [**tree**](https://en.wikipedia.org/wiki/Tree_(data_structure)). And you definitely should modify elements by their id, not by their path in the tree. – Bergi Aug 16 '18 at 19:11
  • Yes, a looping or recursive function is the correct approach (use whichever you are more comfortable with). And start with a function that is *accessing* a value by its path, which is a bit easier than a function storing a value. Can you show us your attempts please? – Bergi Aug 16 '18 at 19:13
  • @NinaScholz Sorry, I should have expressed myself a bit clearer. It's about retrieving the data from the database and rendering. What I want is to get all the posts that are either replies to a specific post or replies to replies to that post and so on. So I need a way to get all the posts that are replies to the specified post, then get all the posts replying to each of those and so on. I tried to write an algorithm that would do that and it should work in theory but I need an object representing a tree of posts and a way to add posts to it when I find new replies. – M. Farnik Aug 16 '18 at 19:24
  • @Bergi Oops, sorry, I'm not really good at terminology. And yeah, it would be easier to use the ID but that's not really an option in this case. I need to be able to eg. get information about the next sibling of the post currently being processed so to use an ID, I'd need an object of IDs that would represent the structure of the tree but in order to make an object like that, I'd need to do exactly what this question is about - use an arbitrarily long array of variables as keys in an object. I'll try to sum up the pseudo-algorithm and post it, give me a sec – M. Farnik Aug 16 '18 at 19:33
  • @M.Farnik The id should *not* be the path in the tree. It should just be a unique identifier. I thought you already had that structure in place: "*it has a property that refers to the ID of the post it's replying to*" – Bergi Aug 16 '18 at 19:35
  • @Bergi ...what? Sorry, I'm a bit confused by that reply. Yes, I already have IDs in place but how does that help me here? Also I never said the id is the path in the tree... I guess I should be a bit clearer next time though, sorry. I did some research and it turns out the pseudo-algorithm I have is really just a depth-first way of getting information about each post. So what I'm really looking for here is a way to implement a recursive depth-first async function in JS. But I also need a way to store the information about the posts I find and to keep track of siblings etc. – M. Farnik Aug 16 '18 at 20:22

1 Answers1

0

i wrote a minor snippet

var obj = [];
var arr = [1,2,3];
var set = 11; // value we want to set to
var test = obj;

for(var k =0 ;k < arr.length; k++) {
    var i = arr[k];
    if(!test[i]) {
        if(k == arr.length-1){
            test[i] = set; }
         else{      
            test[i] = []
         }
    }
    test = test[i]
}

console.log(obj[1][2][3])

you can wrap it in a function for your use case

ashish singh
  • 6,526
  • 2
  • 15
  • 35