2

I have a string cars.type.suv and I'm trying to access the object props but the object is a string.

var cars = {
   type:{
      suv:{
        //watever
      }
   }
};

Question: How can I turn the string cars.type.suv into the object.

Jordan Davis
  • 1,485
  • 7
  • 21
  • 40
  • I imagine: `var cars = {type:{suv:"this is a string, isn't it?"}};` now... what format is the string in? – SparK Apr 20 '16 at 20:01
  • do you want to convert the string into an object with nested levels? – RomanPerekhrest Apr 20 '16 at 20:01
  • @apsillers no simply just the string `cars.type.suv` – Jordan Davis Apr 20 '16 at 20:01
  • @RomanPerekhrest exactly I can't access them, if there was only one level I could just use brackets but there is multiple – Jordan Davis Apr 20 '16 at 20:02
  • This might insterest you: [Convert JavaScript string in dot notation into an object reference](http://stackoverflow.com/questions/6393943/convert-javascript-string-in-dot-notation-into-an-object-reference) – blex Apr 20 '16 at 20:03
  • @SparK no it's object properties whatever they may be, its just accessing multiple nested when only having a string. – Jordan Davis Apr 20 '16 at 20:03
  • @JordanDavis You want to create the object?! split the string by `.` and iterate, checking if the prop is defined in a container so you create objects on your way down and in the last node you set the node name as value too. – SparK Apr 20 '16 at 20:03
  • I think that duplicate answers your question. Let me know if I misunderstood your question, and maybe edit it to clarify and then I can reopen it if you want. – Paul Apr 20 '16 at 20:03
  • @blex yea I already read that one – Jordan Davis Apr 20 '16 at 20:03
  • so, the last element like `.suv` should always be an object, or it sometimes may be an object property? – RomanPerekhrest Apr 20 '16 at 20:03
  • @RomanPerekhrest doesn't matter what object above is simply in example, it's being able to turn a string which is `my.obj.somewhere.with.whatever.nested.levels` – Jordan Davis Apr 20 '16 at 20:05
  • @SparK the object is already created I'm simply trying to access the properties. – Jordan Davis Apr 20 '16 at 20:06
  • This works like a charm for me: ` ` – pid Apr 20 '16 at 20:06
  • 1
    @pid thats not a string... I think everyone knows how to access the the object the way you just listed, you have to realize it's a string `cars.type.suv` – Jordan Davis Apr 20 '16 at 20:08
  • @JordanDavis To be clear: the duplicate answers you question, right? If not, [edit] your question and explain how it does not. Also, this might be a better duplicate: http://stackoverflow.com/questions/6393943/convert-javascript-string-in-dot-notation-into-an-object-reference – apsillers Apr 20 '16 at 20:10
  • @apsillers I just tried it and its good, thanks guys! – Jordan Davis Apr 20 '16 at 20:15
  • Now I understand... you have to split the string at the `.` and then recursively navigate the object tree. That's easy to do but it cannot be written in a comment. – pid Apr 20 '16 at 20:15

0 Answers0