0

I have a little experience in Javascript and usually I found solution to my problems just by searching here or with Google But this time, I feel that I took the wrong path

I have build an Object (multidimensional associative array) like this : state["a"]["b"]["c"] = "idle";

If I know "a" and "c" but not "b", can I change the value "idle" , without looping through each keys ? taking into account that "c" is unique in the whole object

Something like : state["a"][*]["c"] = "busy";

Thanks in advance for your help

3
  • 2
    no you can not, and if you don't know the keys, how can you assure they are to be unique? you have to write a tree parser most likely using recursion. depending on what environment/browser you are to support you can use Object.keys or "for in" loop Commented Apr 16, 2016 at 18:02
  • So you know: there are no "multidimensional" structures in JS. They are simply nested objects/arrays. Commented Apr 16, 2016 at 18:10
  • I know that key is unique as I build the object using some data from which is value can't exist twice ! Just in the current function "b" isn't used nor known ... Too bad, I will loop with Object.keys ... Thanks Commented Apr 16, 2016 at 19:22

1 Answer 1

1

If "c" only exists in one place as you've stated, then you'll need to loop through the items in the second dimension looking for the one that has a third dimension with an index of "c".

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

1 Comment

This is exactly what I would like to avoid :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.