Simple javascript question:
I have an array (50=50; 49=49; 143=143; 4005=4005; … )
which i want to turn into (50; 49; 143; 4005; …).
The name will always be the same as the value, in the name=value pair.
It will always be a number (but of various lengths).
I just cant get my head around it using .split
Thanks
Assuming that you mean you have an array like this:
Then, you may employ
splitlike this:This will result in
newArrbeing equal to this:The way
splitworks is it divides a string up into an array based on a delimiter string. In this example, we’ve used'='as the delimiter, so we end up with arrays like this:Then, index into the first element and pass it to
parseIntto produce a number, andpushonto a new array with just number elements.Here’s a working example.
Addendum
If you aren’t starting with an actual JavaScript array, but a string that you’d like to turn into an array, then add this step before the previous ones to get yourself the original array: