简体   繁体   English

json麻烦,通过变量设置键名

[英]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. 我正在尝试为客户端查询返回一些JSON,但是我需要返回的对象的键基于变量,并且它不起作用。 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. Firefox告诉我错误,缺少:第二行a ['property']的属性ID之前。

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. 这两个函数用于在ajax请求删除文件或用户尝试执行的操作之前获取用户选择的文件列表(这是文件管理器)。 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. 但是对于某些事情,我只想返回每个文件的ID,而不是整个对象的ID,这就是我在这里遇到问题的地方。 For Files, it needs to return an 'id' whereas it should return a 'name' for folders. 对于文件,它需要返回一个“ id”,而它应该为文件夹返回一个“名称”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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