"concat" does not join JavaScript arrays together? -
"concat" does not join JavaScript arrays together? -
i'm running next 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; (var = 0; < 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' 'global' stays empty array. what's going on?
.concat creates new array. need overwrite old one.
scripturls = scripturls.concat(urls); or if want maintain original scripturls array, can .push() values in.
scripturls.push.apply(scripturls, urls); this uses .apply() convert urls individual arguments passed .push(). way content of urls added scripturls individual items.
also, note .concat() flattens array. if wanted array of arrays, you'd utilize scripturls.push(urls).
javascript arrays
great
ReplyDelete