user4383363
user4383363

Reputation:

push method in a loop causing array inside of array

I have two arrays, one full of links and another one of domains of which shall be removed from the first array.

array1 = [ http://www.linkone.com, https://www.linktwo.com, ... ]
array2 = [ 'linkone' ]

The second array does not have the format of a URL, I've done this by doing the following:

for (let a2 of array2) {
  clearedUrls.push(_.pull(array1, `https?:\/\/www.${a2}.*`))
}

It worked, but the output of clearedUrls contains array inside of array:

[
  'https://www.foo.com',
  'https://www.foo.com',
  'https://www.foo.com',
  'https://www.foo.com',
  [
    'https://www.foo.com',
    'https://www.foo.com',
    'https://www.foo.com',
  ],
  [ ... ]
]

I know it's because every iteration it will push. I'd like to know a better way to loop over the array2, remove the URLs from array, and return the array with only links inside, no more arrays.

Upvotes: 0

Views: 48

Answers (1)

Konamiman
Konamiman

Reputation: 50273

You should use concat instead of push:

clearedUrls = clearedUrls.concat(_.pull(array1, `https?:\/\/www.${a2}.*`))

Upvotes: 2

Related Questions