Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8414541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:11:52+00:00 2026-06-10T01:11:52+00:00

I want to change ‘hello’ to ‘hey’ programmatically, the solution should work with any

  • 0

I want to change ‘hello’ to ‘hey’ programmatically, the solution should work with any number of nested elements (I just use 2 levels to keep it simple).

var data = {level1: {level2 : 'hello' }};

I have access to the ‘data’ variable, the path (‘level1/level2’) and the new value (‘hey’).

I tried to do:

var parents = 'level1/level2'.split('/');
var target = data;
for(var i=0; i<parents.length; i++){
   target = data[parents[i]];
}
target = 'hey';

The idea was to travel to the root

target = data

then 1 level deep

target = data['level1'] 

…keep going

target = data['level1']['level2'] //data['level1'] === target

and modify the contents

target = 'hey'

But it looks like a lose the reference to the original object (data) when I do (target = target[‘level2’]).

I guess I can build a string with the path and then evaluate it:

eval("data['level1']['level2']='hey');

Is there a better solution that dosen’t involve eval()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:11:53+00:00Added an answer on June 10, 2026 at 1:11 am

    There are two issues. First is that you keep using data inside the loop, which means you’re trying to access the top level keys instead of the inner keys. Change target = data[parents[i]]; to

    target = target[parents[i]];
    

    The second is that when you change the variable target, you’re not changing the data variable but target instead. If you drop out of the loop one iteration earlier you can update the object which is stored as a reference:

    for(var i=0; i<parents.length-1; i++){
       target = target[parents[i]];
    }
    target[parents[i]] = 'hey';
    

    Demo: http://jsfiddle.net/Lherp/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to change a number to a string like this: var i =
I have a lot lines contains XXXXXXXXX-XXX.XXX number format. I want change number XXXXXXXXX-XXX.XXX
I want to change class name every 3 seconds. Bu it doesn't work. How
I want change the HBox's style when click any object inside this HBox. I
I have number ie 1,15,200 I want change to 115200 because when I divide
I want to change just one field before set the values with PHPExcel: For
If I want change the styling of <a class=brand href=#>Product</a> How I should to
i want change the content of a UITableView when i change a button, how
I want change following javascript code to jquery code, How is it? With done
What I want change the properties(e.g. background) of a context menu in a datatemplate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.