like this object,
var myColumnDefs = [
{key:"label", sortable:true, resizeable:true},
{key:"notes", sortable:true,resizeable:true},......
I can call object as this method, which it works
...
myColumnDefs.key
...
but how to call this object with Stringname like
function myObject (string) {
return myColumnDefs.Stringname
}
alert(myObject('key'));
alert(myObject('sortable'));
thanks for Help
-
Is myColumnDefs an object or array of objects ?Siva– Siva2014年07月01日 06:55:47 +00:00Commented Jul 1, 2014 at 6:55
3 Answers 3
You have to use myColumnDefs[Stringname] Here is the working fiddle And in your case you have to get the element by myColumnDefs[element_index][Stringname]
Comments
First of all, I don't think this will work:
myColumnDefs.key;
You must reference one of the elements in the array:
myColumnsDefs[0].key;
With that in mind you can reference the property by its string name by doing this:
myColumnsDefs[0]['key'];
4 Comments
( instead of [, right after [0]?.. That's better...There are 2 ways of doing this. One way is to change to array to an object with properties where the key name is a property. The other way is to search through the entire array and find the key with the correct name. I would suggest the first way, so the code would look like this:
var keyHolder = {};
for(var i = 0; i < myColumnDefs.length; i ++) {
if(!keyHolder.hasOwnProperty(myColumnDefs[i].key)) {
keyHolder[myColumnDefs[i].key = {
sortable: myColumnDefs[i].sortable,
resizeable: myColumnDefs[i].resizeable
}
}
}
Then you can do this:
keyHolder.label
Hope this helps.
Comments
Explore related questions
See similar questions with these tags.