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?

4
  • I think this will work [searchstring]. Try once Commented Jun 17, 2013 at 4:54
  • 2
    Perhaps eval? (Yes, it's evil, slow and wrong, but it would work) Commented Jun 17, 2013 at 4: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. Commented Jun 17, 2013 at 4:56
  • Ref. stackoverflow.com/questions/15092912/… , stackoverflow.com/questions/6491463/… , stackoverflow.com/questions/13719593/… (resolving is a subtask of updating) Commented Jun 17, 2013 at 5:02

0

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.