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

  • SEARCH
  • Home
  • 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 9021603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:17:38+00:00 2026-06-16T05:17:38+00:00

I am looking to write a function which can look up a value based

  • 0

I am looking to write a function which can look up a value based on a key and replace that value with another. The key is a tree from the start node of JSON. Here is the example.

var myData = {
    name : 'Dan',
    address: {
        city : 'Santa Clara',
                details : {
                   'prevhouse' : ''
                }
    }
}

Input to the function is a key tree. For eg, myData-address-details-prevhouse

When I pass this key with a new value, say ‘Texas’, the prevhouse value will get changed to the new value I am sending.

and new JSON will be

var myData = {
    name : 'Dan',
    address: {
        city : 'Santa Clara',
                details : {
                   'prevhouse' : 'Texas'
                }
    }
}

Here is what I wrote so far

var tree = key.split("-");

now the tree variable contains [“myData”,”address”, “details”,”prevhouse”]

I know that we can access the object using myData[tree[0]][tree[1]][tree[2]], but somehow not able to get it dynamic from parsed value.

how do we generate this dynamically since the length of the depth is not known till runtime.

Hope to get a help.

  • 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-16T05:17:39+00:00Added an answer on June 16, 2026 at 5:17 am

    try with this code:

        var myData = {
            name: 'Dan',
            address: {
                city: 'Santa Clara',
                details: {
                    prevhouse: ''
                }
            }
        };
    
    
        function setAttribute(obj, key, value) {
            var i = 1,
                attrs = key.split('-'),
                max = attrs.length - 1;
    
            for (; i < max; i++) {
                attr = attrs[i];
                obj = obj[attr];
            }
    
            obj[attrs[max]] = value;
            console.log('myData=', myData);
    
        }
    
        setAttribute(myData, "myData-address-details-prevhouse", "Texas");
    

    here a working jsfiddle demo; see the console for the result

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

Sidebar

Related Questions

I wish to write a function which I can add to my .vimrc file
Does anyone know how I can write a Javacript function which returns true if
I'm looking for a function that returns a boolean value if the user is
I am looking for a hashing function that is case insensitive and ignores white
I'm looking to write a Java program which will download a Java source file
I am looking to write application for iPhone which will be able to control
I'm looking to write a stored procedure that takes as a parameter a string
I am looking to write something that seems like it should be easy enough,
I'm looking to be able to extend the function prototype in javascript (that is
I am trying to write a function that returns T/F in regards to if

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.