0

In the code below I can easily enter into a subarray within the array with current = current[0].

var root = [
  [
    'Value 1',
  ]
];
var current = root;

current = current[0];

current.push('Value 2');

document.write(JSON.stringify(root));

How can I do the reverse and go up a level? In this example, current would become:

[['Value 1', 'Value 2']]

The only way I can think of doing this would be looping, but duplicates would be very probably and problematic. All I've found online is looping, which won't work as a mention in the last sentence.

EDIT

I'm looping through a large string and converting it to a tree. Certain characters will require a indent in the tree or a separate branch, while others will require an outdent and return to the parent function. I've achieved the indents by having a current variable just enter a new subarray as suggested here. I just need a way to exit with a different character.

4
  • There is no way to walk back up... Unclear why you would need to do it. What is the actual problem you are trying to solve. Commented Jul 1, 2020 at 21:33
  • You can't, but if you expand on the problem you are trying to solve we might be able to help you with an algorithm Commented Jul 1, 2020 at 21:33
  • @epascarello It is very unclear, sorry. I've edited my answer to try to include more information on why I'm using the approach. Commented Jul 1, 2020 at 21:42
  • @AdrianBrand, Can you check my edited answer? Hopefully it will make more sense. Commented Jul 1, 2020 at 21:43

2 Answers 2

1

You can't.

Arrays have no concept of a "parent" or anything. They don't care where their references are being held. However, if you really really need to, you can implement the concept of a "parent" yourself using a simple recursive function setting properties of the arrays - see below:

var root = [
  [
    'Value 1',
  ]
];

function setParents(root) {
  for(var i = 0; i < root.length; i++) {
    if(Array.isArray(root[i])) {
      root[i].parent = root;
      setParents(root[i]);
    }
  }
}

setParents(root);

var current = root;
current = current[0];
current.push('Value 2');
document.write(JSON.stringify(root));

current = current.parent;
console.log(current);

This makes use of the fact that array properties don't show up when logged or serialized as JSON, only their elements. So they're kind of "hidden" in a sense. It's a bit hacky but could work for you.

However, I recommend you simply avoid overwriting current - there's not really a great need to use it like a cursor traversing some hierarchy structure.

Sign up to request clarification or add additional context in comments.

3 Comments

So you're suggesting that I turn the arrays into objects with a parent value and an actual value? Would it be possible to then edit the parent?
@Jack See my edited answer, there's a sample of a recursive function you could implement on top of your existing code without much hassle.
Thanks for the edit, seems like just what I need. If I were to run current = current.parent and then push a value, would that change root or is it just in the array properties?
0

Simply pushing BEFORE setting current variable will work.

var root = [
  [
    'Value 1',
  ]
];
var current = root;

root.push("AAA");

current = current[0];

current.push('Value 2');

document.write(JSON.stringify(root));

1 Comment

Thanks for your answer, but the rest of my code greatly depends on current so removing would require me to rewrite it (not the worst), but I have no idea how to do it without using a "cursor"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.