Reputation: 21
What's the best way to get a clean array from this:
[{f=[{v=2521998}, {v=0}, {v=99326}]}]
to something like this?
[2521998,0,99326]
Thanks!
Upvotes: 0
Views: 100
Reputation: 87233
As you haven't mentioned the [{f=[{v=2521998}, {v=0}, {v=99326}]}]
is string
or array
, I've included both the answers.
For Array/Object/JSON
json
is not valid(See the correct formatted below)regex
can be used in your case.You can convert the object
to string
using JSON.stringify
and then use regex
to extract the required values from it.
var myArr = [{
f: [{
v: 2521998
}, {
v: 0
}, {
v: 99326
}]
}];
var str = JSON.stringify(myArr);
var resultArr = str.match(/\d+/g);
alert(resultArr);
String
If [{f=[{v=2521998}, {v=0}, {v=99326}]}]
is string
and not object/array
, you don't need JSON.stringify
.
var str = '[{f=[{v=2521998}, {v=0}, {v=99326}]}]';
var resultArr = str.match(/\d+/g);
alert(resultArr);
REGEX Explanation
/
: Delimiters of regex
\d
: Matches any digit+
: Matches one or more of the preceding classg
: Global matches.Upvotes: 5