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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:04:17+00:00 2026-05-27T17:04:17+00:00

I have the following javascript object literal (excerpt) var foo = {hello[35]:100,goodbye[45]:42}; I have

  • 0

I have the following javascript object literal (excerpt)

var foo = {"hello[35]":100,"goodbye[45]":42};

I have the following query:

var query = "hello"

I would like to call foo[query] to obtain the value 100, but there is a [35] for which I don’t necessarily know the value of. I know for sure that I will get a unique match. Is there any way to input query is some kind of javascript regular expression? i.e.

Regex = /hello/
foo[Regex]
100

pardon the noob question…

  • 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-27T17:04:18+00:00Added an answer on May 27, 2026 at 5:04 pm

    What you have here:

    var foo = {"hello[35]":100,"goodbye[45]":42};
    

    is not JSON, which is a string representation of an object; what you have is an object literal, which creates an actual JavaScript object. As far as I know the only way to retrieve a value from an object by matching a property name with a regex is to enumerate the property names and test each one. The regex you’ll need is something like:

    /^hello(\[\d*\])?$/
    

    …which will match against “hello” optionally followed by zero or more digits in square brackets. But you don’t want to hard code “hello” given that you also (presumably) need the “goodbye” value, so use a function:

    function getPropertyByRegex(obj,propName) {
       var re = new RegExp("^" + propName + "(\\[\\d*\\])?$"),
           key;
       for (key in obj)
          if (re.test(key))
             return obj[key];
       return null; // put your default "not found" return value here
    }
    
    var foo = {"hello[35]":100,"goodbye[45]":42};
    
    alert(getPropertyByRegex(foo, "hello"));    // 100
    alert(getPropertyByRegex(foo, "goodbye"));  // 42
    alert(getPropertyByRegex(foo, "whatever")); // null (not found)
    

    Demo: http://jsfiddle.net/asDQm/

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

Sidebar

Related Questions

I have the following javascript: $.post(/Authenticated/DeletePage/ + PageId); showStatus(Page deleted..., 10000); I would like
Assign object literal properties var foo = { bar : 'hello'}; Ternary var cats
I have following resourceBundle Javascript object. var resourceBundle = { en : { MSG1
I have a JavaScript object that looks something like this: var myTextOptions = {
I have the following javascript object containing a multi-value email property: var contact =
I have the following javascript object being returned from a WCF call, this has
Suppose I have the following object in JavaScript: var object = { key1: value1,
In JavaScript, if you have the following code: var map_id = 100; var myobj
I have created the following javascript object dynamically (from PHP): var t_feed = '';
I have the following JavaScript code: oCoord = {x: null, y: null}; var aStack

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.