0

I'm struggling a bit on a common javascript issue, mostly because I don't know the key words to find the solution in Google. What I would like to do in pure javascript :

var obj = {label: 'Name', type: 'text', position: 0};
var name = 'label';

var temp = obj."name" // = obj.label = Name

And use it in meteorJS template :

data = [ 
  {
     "Name" : "Task 1",
     "CreationDate" : ISODate("2014-06-03T19:47:48.252Z"),
     "EndDate" : ISODate("2014-07-03T19:47:48.252Z")
  }, 
  {
     "Name" : "Task 2",
     "CreationDate" : ISODate("2014-06-04T19:47:48.252Z"),
     "EndDate" : ISODate("2014-07-04T19:47:48.252Z")
  }
}

columns = [
        {label: 'Name'},
        {label: 'Creation Date'},
        {label: 'End Date'}
    ]

{{#each data}}
  <tr>
    {{#each columns}}
      <td>{{data.label}}</td>
    {{/each}}
  </tr>
{{/each}}
user2864740
  • 60,010
  • 15
  • 145
  • 220
Nicolas
  • 743
  • 1
  • 7
  • 15
  • 1
    You're looking for ["bracket notation"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors) – elclanrs Jun 04 '14 at 20:59
  • Just to expand on @elclanrs comment, working off the first example you gave: obj[name] or obj['label'] are equal to 'Name' – ArrayKnight Jun 04 '14 at 21:02
  • http://stackoverflow.com/questions/6439915/how-to-set-a-javascript-object-values-dynamically , http://stackoverflow.com/questions/2241875/how-to-create-object-property-from-variable-value-in-javascript?lq=1 – user2864740 Jun 04 '14 at 21:03

1 Answers1

0

So for my first question (pure javascript syntax) its ok. Thanks

var obj = {label: 'Name', type: 'text', position: 0};
var name = 'label';
var temp = obj[name];  //Works fine

For my second question, this way is working (inspired by How to access outer {{#each}} collection value in the nested loop) :

{{#each data}}
    <tr>
        {{#each columns}}
            <td>{{attr .. label}}</td>
        {{/each}}
    </tr>
{{/each}}

Template.worksheets.helpers({
    attr : function (outItem, inItem) {
        return outItem[inItem];
    }
});

But its a bit ugly ...

Community
  • 1
  • 1
Nicolas
  • 743
  • 1
  • 7
  • 15