0

hiiii im creating an application, i get a path from url for access to object properties, and the path is string and i save in variable, i meaning like this code,for example:

but its wrong and throw an error...any body can help me? I'll really appreciate if someone help me. Thanks :)

var object = {
   music:{
      test:"",
      test2:""
   },
   video:{
      test:"",
      test2:""
   },
   pic:{
      test:"",
      test2:""
   },
   photosUsers:{
      test:"",
      test2:""
   }
}

var path = "music.test.test2";
var item = object.path
GreenGiant
  • 4,930
  • 1
  • 46
  • 76
  • Your posted code will not throw an error, but `object.path` will end up being `undefined`. – Rick Hitchcock May 06 '16 at 19:01
  • I recommend to also have a look at http://eloquentjavascript.net/04_data.html and [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196) to get a better of how objects work in JavaScript. – Felix Kling May 06 '16 at 19:44

3 Answers3

2

There are a few problems here.

First, object properties are strings, but you can't just use a dot-separated path to access a nested structure like that. At least not natively. But there are libraries for this. For example, I am the author of dangit, which has a namespace() function to help you do this.

var item = dangit.namespace(object, 'music.test.test2');

Second, even if JavaScript supported a path notation like that, it would probably work like this.

var path = 'music.test.test2';
var item = object[path];

That's because object.path and object[path] are very different things, and there are important differences. Even if you have a path variable, object.path will not use it, because you are asking for a property named path, whereas object[path] gets a property named whatever the value of the path variable is.

Third, object.music.test.test2 does not exist in the data structure you have provided. Use console.log() to learn about this.

console.log('music:', object.music);
console.log('music.test:', object.music.test);

In your example, object.music.test is a String. And since strings don't have a property named test2, trying to access it will return undefined.

Seth Holladay
  • 8,951
  • 3
  • 34
  • 43
  • Why do we need a library for this? Using libraries for something that takes 4 lines of code is why we had the padLeft debacle – amiller27 May 06 '16 at 19:29
  • My `namespace()` function does a great deal more than you are imagining. Also, real world applications need many utilities, so often it makes sense to import a library not just for this, but for other functions as well. If this is somehow the only utility you need, then maybe it doesn't make sense to use a library. Use common sense. As for `left-pad` I am generally [with sindresorhus](https://github.com/sindresorhus/ama/issues/367). There are many issues at play there, and the complexity of the module doesn't affect any of them. Figure out how to trust your dependencies or keep it internal. – Seth Holladay May 06 '16 at 19:47
  • Sorry if I wasn't clear. I would imagine that your `namespace` function does a great deal, and I wasn't trying to imply that it doesn't. I agree with much of what sindresorhus said as well. In this specific case, however, I'm guessing neither the rest of your library nor the full functionality of `namespace` are necessary. – amiller27 May 06 '16 at 19:57
  • No worries! That may be true. `namespace()` can help you _create_ the entire path if it doesn't exist, beyond just _read_ from it. That is more involved. For example, you have to decide whether to overwrite strings/numbers - due to their immutability - if they exist in the middle of the path. I am not sure if the OP cares about that. But dot paths are most commonly used to scaffold out a dynamically computed namespace. Once you have a reference to one, you _usually_ don't need dot paths anymore. So I am making an educated guess about what people will need, even if it is a bit out of scope. :) – Seth Holladay May 06 '16 at 20:38
1

Try this:

var path_array = path.split('.');
var item = object;
for (int i = 0; i < path_array.length; i++) {
    item = item[path_array[i]];
}
amiller27
  • 491
  • 5
  • 8
0
var object = {music : {test : "" , test2 : ""},video : {test : "" , test2 : ""},pic: {test : "" , test2 : ""},photosUsers: {test : "" , test2 : ""}}

//Associate the path variable to the object
object.path = "music.test.test2";
var item = object.path;