Javascript数组操作:是否有更好的方法来执行以下操作?

问题描述

| 我有以下代码可以做到: 从页面上的元素中获取文本 特定元素的过滤器 在\'\\ n \'上分割 删除数组中所有空格的元素 正如您所期望的那样,这似乎比我想要的时间要多一些,并且不会删除数组中所有用空格填充的元素。 仅供参考,然后将两个数组合并为一个,并使用Yepnope加载脚本和样式。此过程大约需要1.5秒,这对于用户来说确实很长。 我该如何提高速度?
var $containerHtml = $(html);

    // Add the scripts
    scriptArray = $.grep($containerHtml.filter(\'#includeScripts\').text().split(\'\\n\'),function (element,index) {
                    return element !== \"\" && element !== \" \";
                });
    // Add the styles
    styleArray = $.grep($containerHtml.filter(\'#includeStylesheets\').text().split(\'\\n\'),index) {
                    return element !== \"\" && element !== \" \";
                });

    // Combine the two arrays into 1
    combinedArrays = scriptArray.concat(styleArray);

    // Load the scripts and styles 
    yepnope([{
        load: combinedArrays,callback: function (url,result,key) {
                if (window.console && window.console.firebug) {
                    console.log(\"Loaded \" + url);
                }
        }}]);
    

解决方法

为什么在html页面中将脚本作为文本数组? 我将它们存储为“ 1”文件在服务器上。
$.when($.getJSON(\"url/script.json\"),$.getJSON(\"url/stylesheet.json\")).then(function(script,style) {
    var arr = ...; // combine script & style. 
    yepnope(...);
});
如果您不希望它们是静态的,则根据传入的URL设置一些路由和服务器json文件。