3

A Javascript object can be used as an associative array. So my question is how can I automatically set that multiple key's with the same ending match one value?

I'll write an example to be more explicit on what I want. Something like this:

var handle = {}
handle[*css] = handlers.style;

Every key that ends with "css" should be matched to handlers.style.

Any way to doing this?

PD: Im doing this with server-side javascript with NodeJS

jviotti
  • 17,881
  • 26
  • 89
  • 148
  • You might be interested in the question [Is there a query language for JSON](http://stackoverflow.com/questions/777455/is-there-a-query-language-for-json) – Richard JP Le Guen Sep 21 '12 at 22:36
  • You can't. Not without either defining your own method or simply iterating over all properties of the object. – James Allardice Sep 21 '12 at 22:36
  • Using node? Look at node-proxy (https://github.com/samshull/node-proxy). You can create a catch-all handler for getters/setters. It can be done. – James Sep 21 '12 at 22:38
  • @999 - Yet another reason for me to finally sit down and get to grips properly with node! I knew of the Harmony proposal but had no idea such a thing existed already. – James Allardice Sep 21 '12 at 22:40
  • hacking the route to check this stuff looked as a cleaner way to achieve it. Thanks anyway – jviotti Sep 21 '12 at 22:55

3 Answers3

4

This is possible if you iterate through the object properties and check that the property name matches the desired pattern. Something like:

for (var i in obj) {
  if (i.toString().substr(-3) === 'css') {
    obj[i] = handlers.style;
  }
}
leepowers
  • 37,828
  • 23
  • 98
  • 129
  • do you think its a waste to check if `obj.hasOwnProperty(i)` first? – lbstr Sep 21 '12 at 22:38
  • When would `i` not be a string? Is `toString()` necessary? – James Sep 21 '12 at 22:40
  • @lbstr `hasOwnProperty` won't detect inherited properties. Which may or may not be desired: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty#Description – leepowers Sep 21 '12 at 22:47
  • @999 The `toString()` isn't needed if you know that `obj` is just an `Object` - and not an `Array` (which may returned numeric keys). It's a bit of defensive programming. – leepowers Sep 21 '12 at 22:48
  • Even though this answers the question, it can't really be used to a route request to a resource to the correct handler. For instance if 'i' is mySuperStyle.css, this will not be routed to the handlers.style – balafi Sep 21 '12 at 23:07
  • @leepowers, `i` will always be a string, even if you're iterating over an array with `for (var i in y)...`. – James Sep 22 '12 at 09:52
1

A quick and dirty way:

var handle = {}
name = "article_users_css"
handle[name.substr(name.length-3)] = "something"

or if you can use some symbol to delimit the suffix, you can use this instead:

name.substr(str.lastIndexOf(YOUR_CHAR_OF_CHOICE))
Alexander Chen
  • 1,075
  • 1
  • 8
  • 18
  • I'm confused. Wouldn't your example just set `handle["css"]` to `"something"`? – lbstr Sep 21 '12 at 22:51
  • Yes it does, apparently I misinterpreted the question. But it does achieve the same result (and possibly with a smaller footprint and easier retrieval), so it could be desirable if a list of the property names been set does not need to be maintained. – Alexander Chen Sep 21 '12 at 22:57
1

I don't think you can do this. But you can the request pathname before routing the request to your handlers. See example below:

 var path = "mystyle.css"
 // var path = "index.html"
 // var path = "image.png"

 var handle = {};
 handle[css] = handlers.style;
 handle[html] = handlers.html;
 handle[img] = handlers.img;

 if (path.match(/.*\.css/)) {
     handle[css]();
 } 
 else if (path.match(/.*\.html/)) {
       handle[html]();
 }
 else if ((path.match(/.*\.png/) || path.match(/.*\.jpg/)) {
       handle[img]();
 }

You can consider using Express that does support routing based on regEx matching

balafi
  • 2,143
  • 3
  • 17
  • 20