0
    ab.c.title =  {};
    ab.c.title2 = {};

    var searchstring = 'ab.c';

    $.each(searchstring, function(x){

    });

I have a string (var = searchstring) which will be used to build the string for the object i want to iterate through in $.each

if i type the object into the 'each' it works.

e.g

    $.each(ab.c, function(x){ };

However if i declare as a string beforehand, this does not work:

    var searchstring = 'ab.c';

    $.each(searchstring, function(x){};

Is there a way to get around this?

Robert Hane
  • 37
  • 1
  • 4
  • I think this will work `[searchstring]`. Try once – Mr_Green Jun 17 '13 at 04:54
  • 2
    Perhaps `eval`? (Yes, it's evil, slow and wrong, but it would work) – Simon M Jun 17 '13 at 04:55
  • Let `searchstring` be "c", then: `ab[searchstring]`. Similar approaches will work for any similar promotion (i.e. put `ab` as a property if needed), as long as there is a "common root" variable - in this case the object graph in question starts at `ab`. More complex cases, i.e. given: x, how to resolve x.a.b from "a.b" are already answered on SO. – user2246674 Jun 17 '13 at 04:56
  • Ref. http://stackoverflow.com/questions/15092912/dynamically-updating-a-javascript-object-from-a-string-path , http://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key , http://stackoverflow.com/questions/13719593/javascript-how-to-set-object-property-given-its-string-name (resolving is a subtask of updating) – user2246674 Jun 17 '13 at 05:02

0 Answers0