This is just an example, I have much more amount of data:
var str = "3.0;4.5;5.2;6.6";
var res = str.split(";");
console.log(res);
The output will be an array of strings. How can I have array of numbers without going through the existing array again?
This is just an example, I have much more amount of data:
var str = "3.0;4.5;5.2;6.6";
var res = str.split(";");
console.log(res);
The output will be an array of strings. How can I have array of numbers without going through the existing array again?
Share asked Dec 14, 2018 at 10:26 user3719454user3719454 1,0161 gold badge12 silver badges27 bronze badges 3- This is an exsisting post: stackoverflow./questions/15677869/… – C4mps Commented Dec 14, 2018 at 10:31
- 4 This question is different from both of those questions, because of the "without going through the existing array again". – T.J. Crowder Commented Dec 14, 2018 at 10:41
- 3 In 99% of cases people add these restrictions because they mistakenly think that avoiding loops will somehow improve performance, when in fact convoluted solutions without loops are often much worse performancewise. – JJJ Commented Dec 14, 2018 at 12:16
5 Answers
Reset to default 6...without going through the existing array again?
That's tricky. You can't with split
, because split
produces an array of strings. You could do it in a single pass with a regular expression, building the array yourself:
var rex = /[^;]+/g;
var str = "3.0;4.5;5.2;6.6";
var match;
var res = [];
while ((match = rex.exec(str)) != null) {
res.push(+match[0]);
}
console.log(res);
Or actually, that's more overhead than necessary, just indexOf
and substring
will do:
var str = "3.0;4.5;5.2;6.6";
var start = 0, end;
var res = [];
while ((end = str.indexOf(";", start)) !== -1) {
res.push(+str.substring(start, end));
start = end + 1;
}
if (start < str.length) {
res.push(+str.substring(start));
}
console.log(res);
KooiInc's answer uses replace
to do the loop for us, which is clever.
That said, unless you have a truly massive array, going through the array again is simpler:
var res = str.split(";").map(entry => +entry);
In the above, I convert from string to number using a unary +
. That's only one of many ways, and they have pros and cons. I do a rundown of the options in this answer.
You can use map()
:
var str = "3.0;4.5;5.2;6.6";
var res = str.split(";").map(Number);
console.log(res);
You could replace all the ;
using regex with ,
and then use JSON.parse()
to convert it to an array of numbers:
var str = "3;4;5;6".replace(/;/g, ',');
var res = JSON.parse("["+str+"]");
console.log(res);
Do note though, in terms of its efficiency, this isn't any better than using a simple .map
to go through the array again.
Another idea without split
:
const str2Numbers = (str, numbers = []) =>
str.replace(/[^;]+/g, a => numbers.push(+a)) && numbers;
console.log(str2Numbers("3.0;4.5;5.2;6.6;3421"));
That's tricky. You can't with split, because split produces an array of strings.
var str = "3.0;4.5;5.2;6.6";
var res = str.split(',').map(function(number) {
console.log(res);
return parseInt(number, 10);
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744922514a4601220.html
评论列表(0条)