Reputation: 43
I have this type of array
[[["80.529299450867271,7.3884550841172976"],["80.528953669541707,7.3875715810979612"],["80.528714422417153,7.3867339810469588"]]]
I want to get this as
[[[80.529299450867271,7.3884550841172976],[80.528953669541707,7.3875715810979612],[80.528714422417153,7.3867339810469588]]]
this is what I have tried
z = a.map(function(o) {
var d = o.split(/(],)/g).map(function(b) {
console.log();
return b;
});
Upvotes: 2
Views: 629
Reputation: 12039
Array.prototype.map() might be used in that scenario. Make an iteration over the array and split the item.
After spiting I used another map to convert the string to number.
const arr = [
[
[
"80.529299450867271,7.3884550841172976"
],
[
"80.528953669541707,7.3875715810979612"
],
[
"80.528714422417153,7.3867339810469588"
]
]
]
const result = [arr[0].map(i => i[0].split(',').map(Number))]
console.log(result)
Upvotes: 2
Reputation: 8660
You can map over the inner array and split the strings, then cast the members of the returned arrays to Numbers.
arr[0] = arr[0].map(([v]) => v.split(",").map(Number));
let arr = [[["80.529299450867271,7.3884550841172976"],["80.528953669541707,7.3875715810979612"],["80.528714422417153,7.3867339810469588"]]];
arr[0] = arr[0].map(([v]) => v.split(",").map(Number));
console.log(arr);
Upvotes: 2
Reputation: 8329
You don't need to remove the quotes - it's not the problem. Try this (with parseFloat()):
const arr = [
[
["80.529299450867271,7.3884550841172976"],
["80.528953669541707,7.3875715810979612"],
["80.528714422417153,7.3867339810469588"]
]
]
const res = arr[0].map(e => e[0].split(',').map(el => parseFloat(el)))
console.log(res)
Upvotes: 0