javascript - trouble with json, setting key name by variable -


var = { property: ((type == 'files') ? 'id' : ((type == 'folders') ? 'name' : '')) };     // have create dummy object property... return_json[type].push({ a['property']: content[type][index][a['property']] }); 

i'm trying return json client-side query, need key of returned object based on variable, , it's not working. under impression creating dummy object work, doesn't.

firefox tells me error, missing : before property id @ a['property'] on second line.

thanks!


here's code in case need it. yes it's largely object oriented. these 2 functions used getting list of files user selected (this file manager) before ajax request delete files or whatever user trying do. things want return id of each file, not entire object, , that's run problem here. files, needs return 'id' whereas should return 'name' folders.

getselectedjson: function(which = 0, onlyselected = true, justids = false) {            // add param 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 = { property: ((type == 'files') ? 'id' : ((type == 'folders') ? 'name' : '')) };     // have create 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 || == 1) {         (var = 0; < (onlyselected ? content.selected.files.length : content.files.length); i++) {             method((onlyselected ? content.selected.files[i] : i),1);         }     }      if (which == 0 || == 2) {         (var = 0; < (onlyselected ? content.selected.folders.length : content.folders.length); i++) {             method((onlyselected ? content.selected.folders[i] : i),2);         }     } }, 

in object literal expression, left side of : construction must either identifier or string constant. cannot expression, you're trying do.

if want close same you've got stylistically, can use anonymous function build object:

  return_json[type].push(function() {     var obj = {};     obj[a.property] = content[type][index][a.property];     return obj;   }()); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -