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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:13:52+00:00 2026-06-05T16:13:52+00:00

I have the following JavaScript data structure. a[] is an array with text strings

  • 0

I have the following JavaScript data structure. a[] is an array with text strings as elements. It is not
known in advance what it contains. For example,

a = ["foo", "bar", "baz"...];

res{} is a JSON object with as many keys as elements in “a” with the following rule; the last innermost key of res{} is an object with
an array called features[] or the last innermost key itself is an array. So, if , then res is either

// if
a = ["foo", "bar"];

// then
res = {
    "foo": {
        "bar": {
            "features": []
        }
    }
};

// and I want
data = res["foo"]["bar"].features;

// or
res = {
    "foo": {
        "bar": []
    }
};

// and I want
data = res["foo"]["bar"];

As I said, it is not know in advance how long a[] is or what the values of its elements is. How can I do the above?

Update: fixed a typo in the structure of res

  • 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-05T16:13:54+00:00Added an answer on June 5, 2026 at 4:13 pm

    This works ok

    function getFeatures(obj, keys) {
      if( keys.length ) {
        key = keys.shift();
        obj = obj[key];
        return getFeatures(obj, keys);
      } else {
        return obj.features || obj;
      }
    }
    

    You may want to call it with a copy of the keys-array, as the function shifts elements off of the array it’s passed. I.e.

    var keys = ["foo", "bar"];
    
    var res1  = { "foo": { "bar": { "features": [1, 2, 3] } } };
    getFeatures(res1, keys.slice(0)); // => [1, 2, 3]
    
    var res2 = { "foo": { "bar": ["a", "b", "c"] } };
    getFeatures(res2, keys.slice(0)); // => ["a", "b", "c"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following example data in javascript: var variations = [ {group: 1,
I have a JavaScript data structure like the following in my Node.js/Express web app:
I have the following code in javascript? if ($(form).data('action') == Edit) { xx }
I have the following JSON data being returned, but for some reason, Javascript or
I have an XML file in the following structure. <NewDataSet> <markers> <name>name text</name> <desc>desc
I currently have the following javascript array: var stuffs = ['a', 'b']; I pass
Let's say I have the following javascript: (function($) { $.getJSON(url, function(data) { $.each(data.rows, function(index,
I currently have the following Javascript, and I am not sure what I should
Let's say for example, I have the following javascript function that returns a boolean:
I have the following javascript: $.getJSON('/calculate_quote/' + moulding_1_id, function(data) { moulding_1_cost = data.moulding.cost; moulding_1_width

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.