简体   繁体   中英

trouble with json, setting key name by variable

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);
        }
    }
},

In an object literal expression, the left side of a : construction must be either an identifier or a string constant. It cannot be an expression, which is what you're trying to do.

If you want something close to the same as what you've got stylistically, you can use an anonymous function to build the object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM