0

I've got an array like this

pages['name'] = "Home";
pages['childs'][0]['name'] = "Sub page 1";
pages['childs'][1]['name'] = "Sub page 2";
pages['childs'][2]['name'] = "Sub page 3";
pages['childs'][2]['childs'][0]['name'] = "Sub sub page 1";

My problem is that I need to change portions of the array for example.

pages['childs'][0] = otherarray;
// or
pages['childs'][2]['childs'][0] = otherarray;

Obviously if otherarray was a string I can easily do something like

eval('pages' + where + ' = "' + stringvalue + '"');

But I've an array as value so I can't do

eval('pages' + where + ' = "' + otherarray + '"');

because the code executed will be

pages['childs'][0] = [object object];

What's the solution? Thanks

7
  • What's the question? pages['childs'][0] = [object object]; is valid if you just alert it out... it just means that pages['childs'][0] contains an object. Commented Apr 9, 2015 at 8:44
  • @JohnGreen I think what he has is a variable where = "['childs'][0]" and he wants to use that to determine what to assign. Commented Apr 9, 2015 at 8:45
  • In pseudo code I've to do something like this. eval('pages' + where) = otherarray; Commented Apr 9, 2015 at 8:46
  • You need to write a parser for where. Then you can iterate down the object levels with those keys, and finally assign otherarray. Commented Apr 9, 2015 at 8:47
  • @Barmar - Then the first issue is that we're attempting to use eval. : ) Commented Apr 9, 2015 at 8:49

2 Answers 2

0

Rather than screw around with eval and stringifying things, you should just build an accessor. Many would agree that using eval like this is just bad practice in every way. I don't know if combining it with stringify makes it worse, but it certainly feels dirty.

Here's a basic, fairly stupid accessor, but it should give you the idea.

// Arguments:  array to modify; new value; series of nested array keys.
function modifyArray(base, value){
   var refObj = base;
   for (var ii=2, max=arguments.length; ii < max; ii++){
       if (!refObj) {
          return false;  // we supplied an invalid key.
       }
       if (ii == max-1){
           refObj[arguments[ii]] = value;
           return true;
       }
       refObj = refObj[arguments[ii]];
   }
   return false; // probably forgot to include keys.
}

modifyArray(pages, otherarray, 'childs', 2, 'childs', 0); 

http://jsfiddle.net/2ts78brg/

Sign up to request clarification or add additional context in comments.

2 Comments

This is not a solution. I need even a parse for the var where.
I don't know where 'where' comes from. At the end of the day, its your code, you can build it and maintain it however you want -- but from my 20 years of code-writing, I can almost guarantee you that your eval/stringify solution will come back and bite you badly at some point.
-1

For me this solution works

eval("pages" + where + " = JSON.parse('" + JSON.stringify(otherarray) + "')");

It sounds more like a workaround then a solution but it works and for me it's enough.

Comments

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.