1

I have an array, basically with a bunch of urls and numbers in this format:

var arr = ["http://example.com", "3", "http://example2.com", "7", ]

I want to split up after each number, so that each array looks like this:

var arr = ["http://example.com", "3"] 

so i can easily compare the number values and organize the list.

I've tried splitting after each number with a regex, but that doesn't do anything, as it's already split, but not into multiple arrays.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
Matt
  • 173
  • 1
  • 13

1 Answers1

1
  1. if you have a bunch of urls and numbers you will get a bunch of arrays if you split according to your description
  2. your initial array has an empty element at the end, you might want to get rid of that unless it was a typo.
  3. What kind of processing do you want to do? If you tell us we can perhaps suggest other ways.

If your description is correct, try this simple script assuming you lost the trailing comma

It will produce

[
  ["http://example.com", "3"],
  ["http://example2.com", "7"]
]

var arr = ["http://example.com", "3", "http://example2.com", "7"];
var newArr = [];
for (var i=0;i<arr.length-1;i+=2) {
  newArr.push([arr[i],arr[i+1]]);
}
alert(newArr);
mplungjan
  • 169,008
  • 28
  • 173
  • 236