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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:01:32+00:00 2026-05-24T05:01:32+00:00

Given data similar to: var data = [{id: 12345, name:’my products’, items:[{ size: ‘XXL’,

  • 0

Given data similar to:

var data = [{id: 12345, 
    name:'my products', 
    items:[{ 
               size: 'XXL', 
               sku: 'awe2345', 
               prices:[{type: 'rrp',prices: 10.99}, 
                        {type: 'sell_price', price: 9.99}, 
                         {type:'dealer', price:4.50} 
               ] 
               },{ 
               size: 'XL', 
               sku: 'awe2346', 
               prices:[{type: 'rep', prices: 10.99}, 
                          {type: 'sell_price', price: 9.99}, 
                          {type:'dealer', price:4.50} 
               ] 
               } 
       ] 
   }] 
}]

is there a way to evaluate a string representation of an element in the data object? for example: “data[0].items[0].prices[0].rrp” …without using 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-05-24T05:01:33+00:00Added an answer on May 24, 2026 at 5:01 am

    The ideal solution would be to not have the string representation in the first place. Seriously ask yourself whether you change the existing code to give you the content in a more ideal output.

    However, if you can’t, this should do what you want:

    var path = "data[0].items[0].prices[0].rrp".split(/[\[\]\.]+/);
    var next = window;
    
    if (path[path.length - 1] == "") {
        path.pop();
    };
    
    while (path.length && (next = next[path.shift()]) && typeof next == "object" && next !== null);
    
    next;
    

    Create a function for this:

    function get(path) {
        var next = window;
    
        path = path.split(/[\[\]\.]+/);
    
        if (path[path.length - 1] == "") {
            path.pop();
        };
    
        while (path.length && (next = next[path.shift()]) && typeof next === "object" && next !== null);
    
        return path.length ? undefined : next;
    }
    

    Downsides:

    1. The variable data must be in the global scope for this to work (cannot be a local variable).

    2. It’s skanky. Use it as a last resort.

    Edit: To use setting functionality, you can abuse the pass-by-reference nature of JavaScript objects as follows:

    function set(path, value) {
        var split = Math.max(path.lastIndexOf("["), path.lastIndexOf("."));
    
        get(path.slice(0, split))[path.slice(split + 1).replace(/\]/, "")] = value;
    }
    

    To provide some explanation of how this works:

    The getter first splits the input into an array, so that each element is a member we need to traverse [data, 0, items, 0, prices, 0, rrp]. If the search string ends with a “]”, we get an extra empty-element at the end of the array, so we check for that and remove it.

    We then do the big loop; so whist we have elements to traverse (while path.length), set the next variable to the next object member we need to traverse next = next[path.shift()]. Check that it’s an object (otherwise it wont have any members to traverse), and check that it isn’t null (because typeof null == “object”).

    Once the loop has executed, we’ll have the final element in the chain; so return it.

    The setter searches for the last object reference in the search string, and retrieves that object reference using the get() function. It then sets the returned objects key to the desired value. If we hadn’t done it this way, we’d have been setting a value rather than a reference, so the “real” object would never get updated.

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

Sidebar

Related Questions

Given a data stream of continuously arriving items containing a timestamp and text (e.g.
For any given file data size, I want to be able to resize (or
Environment: ASP.net MVC: Given anonymous structure as such: var test = new { name
I have the following code 1) var x = {}; x.data = {name: 'name',
Given data like this C1<-c(3,-999.000,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,4,3,6,-999.000) DF<-data.frame(ID=c(A,B,C,D,E),C1=C1,C2=C2,C3=C3) How do I go about removing
Given data frame values are Group year Value A 2010 17 A 2011 18
I am trying to sample a data frame from a given data frame such
Another stupid question here regarding a SQL scenario. Given data such as: FieldKey DocumentKey
Given two data frames: C1<-c(3,4,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,6,3,7,4) DF<-data.frame(C1=C1,C2=C2,C3=C3) DF C1 C2 C3 1 3
Given a data set like this; +-----+---------------------+--------+ | id | date | result |

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.