本文介绍了" CONCAT"没有加入JavaScript数组在一起吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在下面code Webkit的:

I'm running the following code on Webkit:

var scriptElements = document.scripts;
var scriptUrls = [];
// URL matching
var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»""‘’]))/i;
for (var i = 0; i < scriptElements.length; i++) {
    element = scriptElements[i];
    var urls = element.innerHTML.match(regexp);
    console.log('local', urls);
    scriptUrls.concat(urls);
    console.log('global', scriptUrls);
}

我看到后,本地,但全球始终保持为空数组印非空数组。这是怎么回事?

I see non-empty arrays printed after 'local' but the 'global' always stays as an empty array. What's going on?

推荐答案

.concat 创建一个新的阵列。你需要覆盖旧的。

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

scriptUrls = scriptUrls.concat(urls);


或者,如果你想保持原来的 scriptUrls 阵列,可以 .push()中的值。

scriptUrls.push.apply(scriptUrls, urls);

本使用。适用()网​​址转换成传递给个别参数。推()。这样网​​址添加到 scriptUrls 为单个项目。的含量

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.

另外,还要注意 .concat()平坦的数组。如果你想要一个数组的数组,然后你会使用 scriptUrls.push(网址)

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

这篇关于&QUOT; CONCAT&QUOT;没有加入JavaScript数组在一起吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:35