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 8456669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:36:19+00:00 2026-06-10T12:36:19+00:00

Possible Duplicate: Dynamic object property name I want to dynamically generate access to an

  • 0

Possible Duplicate:
Dynamic object property name

I want to dynamically generate access to an object’s property.

If I try to access mydata[i].val.name I get somename.

If I try it like mydata[i] + bar[j] (where bar[j] === ‘.val.name’) it fails.

How do I dynamically create something like this? So that I can access any property of an object using a user generated value?


Some code:

If I have an object I want to be able to iterate through its properties, gathering the ones I am interested in. Ideally I would like something like the following:

var processData = function (data, keys, values) {
  var returnData = [], i, j, k;
  var parsedData = JSON.parse(data);
  var keys = keys || null;
  var values = values || null;
  var datalen = parsedData.length;

  for (i = 0; i < datalen; i++) {
      returnData[i] = {};
      for(j = 0; j< keys.length; j++){
          for(k = 0; k < values.length; k++){
              returnData[i][keys[j]] = parsedData[i] + values;
          }
      }
  }

  return returnData;
};

and then use it like:

var keys = ["foo","bar"];
var values = [".val.name", ".val.date"];
processData(data, keys, values);

But this does not work and in console I see foo="[object Object].val.name" rather than the expected foo="ACME Industries".

  • 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-10T12:36:20+00:00Added an answer on June 10, 2026 at 12:36 pm

    If you want to stick to your pattern of constructing the subscript as a string with dots in it you have to roll your own lookup function, like so:

    function descend(object, sub) {
        var handle = object,
            stack = sub.split('.'),
            history = [],
            peek;
    
        while (handle[stack[0]]) {
            if (peek) {
                history.push(peek);
            }
            peek = stack.shift();
            handle = handle[peek];
        }
    
        if (stack.length > 0) {
            history.push(peek);
            throw "Traversal error, could not descend to '" + stack.join('.') + "' from '" + history.join('.') + "'.";
        }
    
        return handle;
    }
    
    var x = {
        a: {
            b: {
                c: 15
            },
            d: 4
        }
    };
    
    console.log(descend(x, "a"));
    console.log(descend(x, "a.b"));
    console.log(descend(x, "a.b.c"));
    console.log(descend(x, "a.d"));
    
    function processData(data, keys, values) {
        if (keys.length !== values.length) {
            throw "Mismatched keys and value lookups";
        }
    
        var i,
            len = keys.length,
            gathered = {},
            k,
            scratch,
            v;
    
        for (i = 0; i < len; i += 1) {
            k = descend(data, keys[i]);
            scratch = values[i].split('.');
            scratch.shift();
            v = descend(k, scratch.join('.'));
            gathered[keys[i]] = v;
        }
    
        return gathered;
    }
    
    var data = {
        foo: {
            val: {
                name: "ACME Industries"
            }
        },
        bar: {
            val: {
                date: (new Date())
            }
        }
    };
    var keys = ["foo","bar"];
    var values = [".val.name", ".val.date"];
    processData(data, keys, values);
    

    Please note: this will not be nearly as performant as coding without this style of lookup.

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

Sidebar

Related Questions

Possible Duplicate: Dynamic object property name I have an object like this var localAppConfig
Possible Duplicate: How to access object using dynamic key? I have multiple select with
Possible Duplicate: Getting static property from a class with dynamic class name in PHP
Possible Duplicate: Javascript dynamic variable name A very basic question. I want to create
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? I have
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? Is there
Possible Duplicate: Auto-size dynamic text to fill fixed size container How can I get
Possible Duplicate: Dynamically loading an external CSS file I'm trying to create a dynamic
Possible Duplicate: Use javascript variable in object name I am using CKeditor as a
Possible Duplicate: Dynamic loading of python modules python: How to add property to a

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.