【问题标题】:Unexpected token { when add object to an array with .map意外的令牌 { 当使用 .map 将对象添加到数组时
【发布时间】:2015-08-01 22:39:31
【问题描述】:
var $scope={};
var componentsDir="/root/";
var appPrefix="/app/";


var scriptRef=[];
function proDir(scriptName){
    return componentsDir+appPrefix+'-home-components/pro/js/'+scriptName+'.js';
};

var scriptList =[
            {s_name:'jquery',file:"jquery.js"},
            {s_name:'bootstrap',file:"bootstrap.min.js"},
            {s_name:'easing',file:"jquery.easing.min.js"},
            {s_name:'fittext',file:"jquery.fittext.js"},
            {s_name:'wow',file:"wow.min.js"},
            {s_name:'creative', file:"creative.js"},

            /*{bootstrap :"bootstrap.min.js"},
            {easing :"jquery.easing.min.js"},
            {fittext :"jquery.fittext.js"},
            {wow :"wow.min.js"},
            {creative :"creative.js"},*/
        ]



var newscript = scriptList.map(function(scriptItem){
    console.log(scriptItem)
    return {{scriptItem.s_name:'jquery'},{scriptItem.file:proDir(scriptItem.file)}},
});

console.log(newscript)

我试图找到一种方法来循环遍历脚本列表并使用.map 为每个元素添加额外的目录信息。但我收到一个错误

Uncaught SyntaxError: Unexpected token {

我尝试将每个元素作为新 newscript 数组的对象返回

【问题讨论】:

    标签: javascript arrays object dictionary


    【解决方案1】:

    { 不是 JSON 中 { 之后的有效字符。

    替换这一行:

    return {{scriptItem.s_name:'jquery'},{scriptItem.file:proDir(scriptItem.file)}}
    

    用这一行:

    return {"scriptItem.s_name": 'jquery', "scriptItem.file": proDir(scriptItem.file)}
    

    或其他变体可能是:

    return {
        scriptItem: {
            s_name: 'jquery',
            file: proDir(scriptItem.file)
        }
    };
    

    这是您在评论中要求的变体“我想像这样访问 newscript 的位置值:newscript.jquery

    var newscript = scriptList.map(function(scriptItem) {
        var returnval = {};
        returnval[ scriptItem.s_name ] = scriptItem.file;
        return returnval;
    });
    

    我认为您遇到了这个问题:

    How can i name object "keys" programmatically in JavaScript?

    如有疑问:

    http://jsonlint.com/

    【讨论】:

    • 现在出现“Uncaught SyntaxError: Unexpected token .”的错误。
    • @Chen 我更新了答案来解决这个问题。问题是需要引用带有特殊字符的键
    • 但我想像这样访问 newscript 的位置值:newscript.jquery 然后我去 dir 位置。相反,现在我必须做 newscript[0].file。我确实将返回更改为返回 { [name] :file},但我仍然必须执行 newscript[0].jquery。我想知道我是否可以删除 [0] 并使用一些方法,“jquery”作为索引?谢谢
    • 啊,我现在明白你在追求什么了。我更新了答案。检查最后一个变体。诀窍在于为您的 javascript 对象使用括号表示法,这允许您在设置键时评估语句。
    【解决方案2】:

    map() 方法创建一个新数组,其结果是对该数组中的每个元素调用提供的函数。

    这意味着每次地图迭代中的 scriptItem 变量都是您在 scriptList 数组中拥有的对象之一。 Map() 对该变量进行操作,并返回一个新变量,该变量将被推送到一个新数组中,scriptItem 将指向该数组。

    在您的代码中,您将返回 2 个对象,而不是一个。但是每个数组元素只能容纳一个对象。所以,只返回一件事。

    var newscript = scriptList.map(function(scriptItem){
        console.log(scriptItem)
        return {s_name:'jquery', file:proDir(scriptItem.file)}
    });
    

    【讨论】:

    • P.S.官方文档是你最好的朋友 :)
    【解决方案3】:

    这应该为您完成工作:

    var newscript = scriptList.map(function(scriptItem){
        console.log(scriptItem);
        var name = scriptItem.s_name;
        var file = proDir(scriptItem.file);
        return {s_name: name, file:file}
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 2017-11-06
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      相关资源
      最近更新 更多