-2

Here is a js object that represents the file system in the command line os project I'm working on:

var obj = {
        "1": {
            "hi": "hi"
        }, 
        "2": {
            "bye": "bye"
         }
    };
var currentDir = obj["1"]["hi"];
console.log(currentDir);

When I run this, I get

"hi"

How do I get this to appear as

/1/hi/

I need to get the "file path" of the currently select object.

Chase
  • 121
  • 2
  • 4
  • 15
  • 2
    Your code logs `"hi"` when I run it, not `"[object Object]"`. – gen_Eric Jun 26 '13 at 14:31
  • THat doesn't make any sense. Javascript does not record which properties refer to a value. – SLaks Jun 26 '13 at 14:31
  • It works for me, but try `obj[1].hi` – casraf Jun 26 '13 at 14:32
  • So, what *exactly* are you trying to do? What's the issue here? – gen_Eric Jun 26 '13 at 14:34
  • 1
    @Chase Welcome to StackOverflow. It's not very clear what you're trying to do, because **it doesn't seem like you have an understanding of the language constructs** that you're using. I recommend doing some reading about javascript objects and arrays, and the accessors for each. When you have a better understanding, come back and revise your question. – xdumaine Jun 26 '13 at 14:34
  • I'm sorry this i s poor wording on my part, I need to "get the file path" of the object – Chase Jun 26 '13 at 14:36
  • 1
    @Chase: That is fundamentally impossible. The same string can be referenced by multiple objects. You need to re-think your design. – SLaks Jun 26 '13 at 14:37
  • 1
    @Chase, as you're entering `"1"` and `"hi"` manually, why can't you just do `"1" + "/" + "hi" + "/"`? – Paul S. Jun 26 '13 at 14:39
  • Unclear.. something like this; http://jsfiddle.net/WmSKL/ ? – Alex K. Jun 26 '13 at 14:39
  • 1
    Don't you already know the path when you address the object? do something like `console.log(firstIndex + '/' + secondIndex + '/ + obj[firstIndex][secondIndex]);` – Yotam Omer Jun 26 '13 at 14:43
  • that's the idea. I don't know how to split the currentDir into those parts. – Chase Jun 26 '13 at 14:44
  • @Chase: You can't. Where are `"1"` and `"hi"` coming from? Once you do `obj["1"]`, you can't go back and get the key (`"1"`). – gen_Eric Jun 26 '13 at 14:49

3 Answers3

3

Make some kind of lookup function

var lookup = (function (o) {
    return function lookup() {
        var i, e = o, s = '';
        for (i = 0; i < arguments.length; ++i) {
            s += '/' + arguments[i];
            if (!e.hasOwnProperty(arguments[i]))
                throw "PathNotFoundError: " + s;
            e = e[arguments[i]];
        }
        return {path: s, value: e};
    }
}(obj));

And using it

console.log(lookup('1', 'hi').path); // "/1/hi"
Paul S.
  • 64,864
  • 9
  • 122
  • 138
0

Your code returns "hi" So does var currentDir = obj[1].hi;

Alex Marinov
  • 191
  • 1
  • 5
0

You already know the path when you access your object. do something like this:

console.log(firstIndex + '/' + secondIndex + '/ + obj[firstIndex][secondIndex]);

you can use this in for loops, each loops while etc.. or by direct access like your example.

Yotam Omer
  • 15,310
  • 11
  • 62
  • 65