0

I have a path to an objects attribute in text, and I need to set some value on that path, but on the real object

Example:

    var path = 'object.children.child[0].id';
    var id = 1;

And I need to set it on the id on the attribute located on path

  • If it weren't for the `[0]` part, you could use the solution here: http://stackoverflow.com/questions/10934664/convert-string-in-dot-notation-to-get-the-object-reference. You could convert `[0]` to `.0` and then us that solution. – Barmar Oct 03 '14 at 01:06
  • I think `eval(path + ' = ' + id)` should work. – Jared Farrish Oct 03 '14 at 01:07
  • Thanks guys, you rock!!, I was actually using eval before asking, but just found out that since I was trying to assign text value, I was missing the quotes, it ended up like this eval(path + ' = "' + value + '"'); – Inconsiderable Arse Oct 03 '14 at 01:29
  • @SalvadorVillegas Nice you could solve it. – renatoaraujoc Oct 03 '14 at 15:58

1 Answers1

1

Use eval(), it interprets strings as Javascript code. People say it's unsecure, but it really depends on how you expose it.

renatoaraujoc
  • 321
  • 2
  • 15