Assuming that you mean you have an array like this:
var arr = ['50=50;','49=49;','143=143;','4005=4005;'];
Then, you may employ split
like this:
var newArr = [], ii;
for (ii = 0; ii < arr.length; ii += 1) {
newArr.push(parseInt(arr[ii].split('=')[0], 10));
}
This will result in newArr
being equal to this:
var newArr = [50, 49, 143, 4005];
The way split
works 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:
['50', '50;']
['49', '49;']
// etc.
Then, index into the first element and pass it to parseInt
to produce a number, and push
onto 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:
var str = '(50=50; 49=49; 143=143; 4005=4005;)';
var arr = str.replace(/\(|\)|;/g, '').split(' ');
(50=50; 49=49; 143=143; 4005=4005; ... )
don't exist in JavaScript. We cannot help you if we don't know what you actually have. – Felix Kling May 9 '12 at 12:34