3

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...

ejang
  • 3,982
  • 8
  • 44
  • 70

6 Answers6

6

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/

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • thanks. darn, i was hoping there it was a quick 1-liner thing so i didn't have to search the javascript object literal by myself – ejang Dec 25 '11 at 18:32
  • No one-liner once you have an object - that's why I'd write it as a function. But are you creating the object by parsing a JSON string? If so you could do a once-off find/replace _before_ you parse it to remove the bits in square brackets. – nnnnnn Dec 25 '11 at 23:10
2

Not sure if you can use regex without any plugins or so ... This might help already ...

var foo = {"hello[35]":100,"goodbye[45]":42};
var query = "hello";
for(var key in foo){
    if (key.indexOf(query) > -1)
        document.write(foo[key]);
}

http://jsfiddle.net/3qqSr

Tim Vermaelen
  • 6,869
  • 1
  • 25
  • 39
  • JavaScript definitely supports regular expressions, but +1 for a simple non-regex solution. (Of course this assumes there won't also be a property like "hellothere[62]" or "shelloil[19]", so it might be more reliable to test on `key.indexOf(query + "[") === 0`.) The regex version of this is what my overcomplicated answer provides. – nnnnnn Dec 25 '11 at 11:21
  • Yep. Sorry, I think I was editing my comment to say something similar at the same time you were entering your comment. – nnnnnn Dec 25 '11 at 11:28
  • exactly :) you gave me the same idea ^^ – Tim Vermaelen Dec 25 '11 at 11:35
0

I am noob here too but I have seen this page previously see it helps you with your question. It basically explains JSon path. Also see this question.

Community
  • 1
  • 1
Shadow
  • 6,161
  • 3
  • 20
  • 14
0

As your JSON is a string, you can use a regexp with this kind of statement:

var foo = '{"hello[35]":100,"goodbye[45]":42}';
var result = foo.match(/"hello\[\d*\]":\d*/g);
result = result[0].split(":")[1];
alert(result);

See it live on jsfiddle

Note that you could use a var instead of "hello" in your regexp.

JMax
  • 26,109
  • 12
  • 69
  • 88
0
var foo = {"hello[35]":100,"goodbye[45]":42};

foo = foo.replace(/\[\d+\]/g,'');

var obj = (new Function("return "+foo))();

obj.hello -> 100

obj.goodbye -> 42

var query = 'hello';

obj[query] -> 100
  • Your initial `foo` declaration is an object literal, not a string (and not JSON). Which is good because that's what was in the question, but bad because it means you can't use the string `.replace()` method on it. – nnnnnn Dec 25 '11 at 11:15
0
function getVal(s, q){
   var r = s.match(new RegExp(q + "\\[\\d*\\]\":(\\d*)[\\,\\}]"));
   return r?r.pop():false;     
}
getVal(foo, "hello")
Diode
  • 24,570
  • 8
  • 40
  • 51
  • In your function you should say `s.match()` - except it won't work anyway because `foo` isn't a string... – nnnnnn Dec 25 '11 at 12:08