1

I have a function, which iterates over some objects, and I want to then use the variable name of the object being iterated over. Currently I maintain a duplicate list of names, and refer to them by array index. This seems unnecessary. The whole thing is in an enclosure.

In principle, I can see two ways to do it.

One is to use the list of names, and somehow refer the the variables named such, and the other is to somehow determine the variable name from the variable itself (held in an array).

Is this possible, or should I be looking at a completely different approach?

(function(){

  var a = {p:true,b:true};
  var b = {em:true,i:true};
  var c = {h1:true,strong:true};

  var x = function(tagName){

    var typedefnames = ["a","b","c"]
    var typedefs = [a,b,c];

    var types = {}
    var i;

    for( i=0; i<typedefs.length; i++ )
      if( typedefs[i][ tagName ] )
        types[ typedefnames[i] ] = true
      else
        types[ typedefnames[i] ] = false

    return types; 

  }

  console.log(x("p"))
  // { a:true, b:false, c:false }

}())

3 Answers 3

2

Do you really need three variables? I suggest using a single object instead, and its keys would do the role of your current variable names:

(function(){
    var obj = {
        a : {p:true,b:true},
        b : {em:true,i:true},
        c : {h1:true,strong:true}
    };

    var x = function(tagName){
       var types = {}
       for(var key in obj) {
           types[key] = obj[key].hasOwnProperty(tagName) && obj[key][tagName] === true;
       }
       return types; 
    }
    console.log(x("p"));
}());

http://jsfiddle.net/sKbPu/1/

Sign up to request clarification or add additional context in comments.

Comments

1

If you have freedom over the objects you could try this

(function(){
  var a = {name: 'a', tags: {p: true, b: true}};
  var b = {name: 'b', tags: {em: true, i: true}};
  var c = {name: 'c', tags: {h1: true, strong: true}};

  var x = function(tagName){
    var typedefs = [a, b, c];

    var types = {};

    for(var i=0; i<typedefs.length; i++ ) {
      if(typedefs[i].tags[tagName]) {
        types[typedefs[i].name] = true;
      }
      else {
        types[typedefs[i].name] = false;
      }
      //alternative way for setting true/false based on truthy value
      //types[typedefs[i].name] = !!typedefs[i].tags[tagName];
    } 

    return types; 
  }

  console.log(x("p"))
  // { a:true, b:false, c:false }
}())

Comments

0

Although not perfect to me (as there is a small amount of duplication still) I think this might be the cleanest solution.

(function(){

  // leave these as they are as they can be used from many other parts of the code
  var a = {p:true,b:true};
  var b = {em:true,i:true};
  var c = {h1:true,strong:true};

    var x = function(tagName){
       // define a single object with key/value pairs that can both be accessed
       var typedefs = {a:a,b:b,c:c}
       var types = {};
       // iterate the type definitions, setting the value based on lookup of originals
       for(var key in typedefs)
           types[key] = !!typedefs[key][tagName];
       // good to go!
       return types; 
    }

    console.log(x("p"));
    // { a:true, b:false, c:false }

}());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.