var a = { property: ((type == 'files') ? 'id' : ((type == 'folders') ? 'name' : '')) }; // have to create a dummy object property...
return_json[type].push({ a['property']: Content[type][index][a['property']] });
I'm trying to return some JSON for a client-side query, but I need the key of the returned object to be based on a variable, and it's not working. I was under the impression that creating a dummy object would work, but it doesn't.
Firefox tells me error, missing : before property ID at a['property'] on the second line.
Thanks!
Here's all the code in case you need it. Yes it's largely object oriented. These two functions are used for getting the list of files the user selected (this is a file manager) before an ajax request to delete the files or whatever the user is trying to do. But for some things I only want to return the ID of each file, not the entire object, and that's where I run into a problem here. For Files, it needs to return an 'id' whereas it should return a 'name' for folders.
GetSelectedJSON: function(which = 0, onlyselected = true, justids = false) { // add another param to only get IDs!
var return_json = { files: [], folders: [] }
this.ForEachItem(which, onlyselected, function(index,type) {
var type = ((type == 1) ? 'files' : ((type == 2) ? 'folders' : function(){ return; }));
if (justids) {
var a = { property: ((type == 'files') ? 'id' : ((type == 'folders') ? 'name' : '')) }; // have to create a dummy object property...
return_json[type].push({ a['property']: Content[type][index][a['property']] });
} else {
return_json[type].push(Content[type][index]);
}
});
return JSON.stringify(return_json);
},
ForEachItem: function(which = 0, onlyselected = true, method = function(index,type){}) {
if (which == 0 || which == 1) {
for (var i = 0; i < (onlyselected ? Content.selected.files.length : Content.files.length); i++) {
method((onlyselected ? Content.selected.files[i] : i),1);
}
}
if (which == 0 || which == 2) {
for (var i = 0; i < (onlyselected ? Content.selected.folders.length : Content.folders.length); i++) {
method((onlyselected ? Content.selected.folders[i] : i),2);
}
}
},