“concat” does not join JavaScript arrays together?

前端 未结 2 2114
傲寒
傲寒 2020-12-11 02:17

I\'m running the following code on Webkit:

var scriptElements = document.scripts;
var scriptUrls = [];
// URL matching
var regexp = /\\b((?:[a-z][\\w-]+:(?:\         


        
2条回答
  •  感动是毒
    2020-12-11 02:42

    .concat creates a new Array. You need to overwrite the old one.

    scriptUrls = scriptUrls.concat(urls);
    

    Or if you want to keep the original scriptUrls Array, you can .push() the values in.

    scriptUrls.push.apply(scriptUrls, urls);
    

    This uses .apply() to convert urls into individual arguments passed to .push(). This way the content of urls is added to scriptUrls as individual items.


    Also, note that .concat() flattens the Array. If you wanted an Array of Arrays, then you'd use scriptUrls.push(urls).

提交回复
热议问题