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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:16:45+00:00 2026-05-24T08:16:45+00:00

var arr = { foo : 1, bar: { baz : 2 }, bee

  • 0
var arr = { foo : 1, bar: { baz : 2 }, bee : 3 }

function getter(variable) {
  return arr[variable];
}

If I want ‘foo’ vs ‘bee’ I can just do arr[variable] – that’s easy, and the function does that.

But what if I want to get arr.bar.baz AKA arr[bar][baz]?

What can I pass to the getter function that will let me do that, (and of course also let me get non-nested properties using the same function).

I tried getter('bar.baz') and getter('[bar][baz]') but those didn’t work.

I suppose I can parse for dots or brackets (like here: In javascript, test for property deeply nested in object graph?). Is there a cleaner way? (Besides eval of course.)

Especially because I need to get the deeply set properly many many times in a loop for a bunch of array elements.

  • 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-24T08:16:47+00:00Added an answer on May 24, 2026 at 8:16 am

    How about change the getter function signature as getter('bar', 'baz') instead

    function getter() {
      var v = arr;
      for(var i=0; i< arguments.length; i++) {
        if(!v) return null;
        v = v[arguments[i]];
      }
      return v;
    }
    

    ps. didn’t test, but you get the idea 😉

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

Sidebar

Related Questions

Lets assume we have the following array var arr = new string[] {foo,bar,jar,\r,a,b,c,\r,x,y,z,\r); Also
var arr = [foo,bar,xyz]; arr[arr.indexOf('bar')] = true; Is there an easier way to do
function whatTheHeck(obj){ var arr = [] for(o in obj){ arr.concat([what]) } return arr }
I want to declare multiple variables in a function: function foo() { var src_arr
so I have: var something = function () { return 6} var foo =
var foo = { someKey: someValue }; var bar = someKey; How do I
I have the following : <script> $().ready(function() { $(#myVal#).blur(function() { var arr = jQuery.makeArray(
var arr = [1,2,3,5,6]; Remove the first element I want to remove the first
var arr = [[1,2,3],[4,5,6],[7,8,9]]; function out(ar){ var interval = setInterval(function(){ for (var i=0; i<ar.length;i++){
$(document).ready(function() { $('a.menuitem').click(function() { var arr = 0; var link = $( this ),

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.