2
\$\begingroup\$

I've got a structure of files and folders in a directory structured like the "example_object" I describe below. I also have paths of each of the files as strings.

So my solution is to split the path into an array, and then use the array to index the object as described in the function below. The length of the array will vary as I don't know how many levels of folders there will be.

Whilst what I've written below works, and I guess is only 4 lines, I see a lot of users get anxious about using "eval". It also seems an indirect method to get the job done. Any improvements/alternate ways to do this?

function change_object_using_path_string(this_object,this_path,new_value){
  var array_as_index = this_path.split("/");    
  array_as_index = "['"+array_as_index.join("']['")+"']";
  var code_to_eval = this_object+array_as_index+"='"+new_value+"'";
  eval(code_to_eval);  
}

The above function is what I'm actually using in my script. The below code reflects the structure of the inputs I would put into the above function:

// NOTE: I'm only inputting "example_object" into 'change_object_using_path_string' as a string to facilitate the eval solution I'm using. I would prefer a solution which directly inputs example_object into the function.

example_object = {
  folder_1_name:{
    folder_2_name:{
      folder_3_name:{
        'file.txt':'this file content'  
      }
    }
  }
}
example_path = "folder_1_name/folder_2_name/folder_3_name/file.txt";

change_object_using_path_string("example_object",example_path,"new content instead");

\$\endgroup\$
9
  • 4
    \$\begingroup\$ There's at least 2 problems with this question: 1) it isn't working as intended. On Code Review, code not working as intended is not fit for review and 2) it's example code. This is not your actual code. Examples are off-topic at Code Review, since the chance of writing an answer you gain nothing from is too high and the context required for a good answer is missing. Please take a look at the help center. \$\endgroup\$
    – Mast
    Jan 10, 2018 at 11:01
  • \$\begingroup\$ @Mast Sorry - I can't currently ask questions on StackOverflow (where I think the format would have been acceptable?), and formatted it for it. Just out of curiosity, would this question have been acceptable on Stack Overflow as is? I've tried to improve my old questions on it to regain the right to ask questions, but I can't see anything more I can do. \$\endgroup\$
    – user140724
    Jan 10, 2018 at 11:13
  • 4
    \$\begingroup\$ This is not Stack Overflow. Posting it here because you couldn't post it where you should've posted it is something you really shouldn't even consider. Do that at your work once too often and nobody will want to help you. It may have worked over there, but a little clarification wouldn't have hurt. The length of the index variable can vary. Yea, so? How often? When exactly? What are you using this solution for that your index varies halfway the look-up? There may be a better solution out there, depending on what exactly you're trying to do and why. \$\endgroup\$
    – Mast
    Jan 10, 2018 at 11:21
  • \$\begingroup\$ Ok, point taken. I'll be more careful to check the guidelines before posting on any of the stackexchange websites. Apologies for misusing codereview. Thanks for the suggestions about how to better format the question for SO. \$\endgroup\$
    – user140724
    Jan 10, 2018 at 11:29
  • 1
    \$\begingroup\$ @Graipher Thanks, that makes sense, I've put the example input at the end clarifying it's not part of the code that I use, whilst the function is code I'm using. \$\endgroup\$
    – user140724
    Jan 17, 2018 at 17:18

1 Answer 1

1
\$\begingroup\$

Yes, it's fairly straightforward to do this without using eval. For example, a statement like obj['a']['b']['c']['d'] = 'value'; can be rewritten as a series of smaller steps:

var temp = obj;
temp = temp['a'];      // member access
temp = temp['b'];      // member access
temp = temp['c'];      // member access
temp['d'] = 'value';   // assignment

Now, it shouldn't be too hard to see how those member access statements can be rolled into a loop, if you have an array of indexes (which you do).


A few other notes:

  • It's good to see descriptive names, but change_object_using_path_string strikes me as a bit too long and unwieldy. What about something like set_path_value?
  • You may want to use some more spacing between function parameters (param1,param2 vs param1, param2) and operators (a+b vs a + b). This makes individual parts stand out more, making the code a little easier to read.
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the comments about clarity also :-) \$\endgroup\$
    – user140724
    Jan 19, 2018 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.