简体   繁体   English

当我的配置文件中有一个普通数组时,我应该如何 go 关于使用 jQuery 扩展?

[英]How should I go about using jQuery extend when my config file has a normal array in it?

Seems $.extend uses only the keys of its input to determine what to overwrite.似乎$.extend仅使用其输入的键来确定要覆盖的内容。 So when my config looks like this所以当我的配置看起来像这样

var config = {
    "numeric" : false,
    "keycode_whitelist" : [
        37, 39, // Left, right
        9,      // Tab
        17,     // Ctrl
        116     // F5
    ]
};

and is extended with more keycodes to add to the whitelist, extend simply overwrites the defaults with the new keycodes one by one even though they are different values.并且扩展了更多的键码以添加到白名单中,即使它们是不同的值,扩展也会用新的键码一一覆盖默认值。

I'm thinking about solving this problem by typing the keys like this 37: 37, 39: 39 etc. I would love a solution that doesn't force me to mess up the syntax of my configuration though.我正在考虑通过键入37: 37, 39: 39等键来解决这个问题。我想要一个不会强迫我弄乱配置语法的解决方案。

You might want to use merge instead of extend:您可能想要使用合并而不是扩展:

var config = {
    "numeric": false,
    "keycode_whitelist": [
        37, 39, // Left, right
        9, // Tab
        17, // Ctrl
        116 // F5
    ]
};

var custom = {
    "somevalue": "some other things",
    "keycode_whitelist": [
        1, 2, 3
        ]
};
var newopts = $.extend({}, config, custom);
newopts.keycode_whitelist = $.merge(custom.keycode_whitelist, config.keycode_whitelist);

Demo: http://jsfiddle.net/3Q4cF/2/演示: http://jsfiddle.net/3Q4cF/2/

Update:更新:

To merge every single array:要合并每个数组:

$.each(config, function(key, obj){
    if($.isArray(obj)) {
        if(custom[key]) {
           newopts[key] = $.merge(config[key], custom[key]);
        }
    }
} );

http://jsfiddle.net/3Q4cF/5/ http://jsfiddle.net/3Q4cF/5/

暂无
暂无

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

相关问题 我应该如何在该多维数组中使名称变为粗体? - How should I go about making the names bold in this multidimensional array? 我应该如何 go 关于使用 for 循环将 arrays 连接到另一个数组并生成结果? - How should I go about using a for loop to randomize arrays concat into another array and generate the outcome? 我应该如何使用模块模式处理一个较长的JavaScript文件? - How should I go about a long JavaScript file using the module pattern? 我应该如何解决这个问题? - How should I go about solving this issue? 使用jQuery时,我应该关心JavaScript引擎的速度吗? - Should I care about JavaScript engine speed when using jQuery? 使用javascript和vue将对象添加到数组时,应该怎么办? - How should i go when adding an object to an array using javascript and vue? 如何使用Google App脚本将用户选择的文件上传到Google云端硬盘中的文件夹? - How would I go about uploading a user-chosen file to a folder in my Google Drive using Google App Script? 当在 React 中单击一个孩子时,我应该如何 go 关于更改兄弟姐妹的 state? - How should I go about changing the state of siblings when a child is clicked in React? 我应该如何使用PHP Excel将MYSQL数据导出到Excel中 - How should I go about exporting MYSQL data into Excel using PHP Excel 我应该如何本地化现有的JavaScript Web应用程序? - How should I go about localising an existing JavaScript web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM