How to transform a string of maps coordinates into an array javascript? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am drawing trails on a map and the coordinates of the trail are saved as string in a json file in the following format:
(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)
I need to get these values and add them to an array:
coordinates= [43.886758784865066, 24.226741790771484,43.90271630763887, 24.234981536865234];
So how can I do this transition?

You can try that way
var string = '(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)';
string.match(/\d+(\.\d+)/g).map(function(d){return d;});

You could just use regular expressions to parse those strings.
const match = string.match(/\((.*)\, (.*)\),\((.*)\, (.*)\)/)
/*
Matches
["(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)", "43.886758784865066", "24.226741790771484", "43.90271630763887", "24.234981536865234"]
*/
const Array.prototype.slice.call(match).splice(1, 4)
/* Converts to array and takes the last three elements
["43.886758784865066", "24.226741790771484", "43.90271630763887", "24.234981536865234"]
*/

Related

How to parse object array that has been stringified using backslashes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I have been searching for the correct regex for converting this string to JSON. It works if there is a single object in the array, but I believe the comma is messing it up somehow.
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
const obj = JSON.parse(json.replace(/("[^"]*"\s*:\s*)(\d{17,})/g, '$1"$2"'));
console.log(obj);
There was nothing wrong except that the list of objects was not contained within square brackets
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
console.log(JSON.parse(`[${json}]`))

Remove First and Last double Quotes from "["Morning Shift","Day Shift"]" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to remove First and Last Quotes from this array Using JavaScript:
"["Morning Shift","Day Shift"]"
After Remove quotes output look like this
["Morning Shift","Day Shift"]
You can try using JSON.parse():
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
Demo:
var data = `["Morning Shift","Day Shift"]`;
data = JSON.parse(data);
console.log(data);

REMOVE SPECIFIC NUMBER WITH JS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'M TRYING TO HANDLE THIS DATA IN JAVASCRIPT SO HOW CAN I REMOVE DATA ACCORDING TO THE ID NUMBER THE FIRST NUMBER
600030/01/2018/163904
600010/01/2014/3789
600030/01/2020/47104
600030/01/2012/39104
600010/01/2011/93817
how can i remove all 600030 data so i get
600010/01/2014/3789
600010/01/2011/93817
If all data shaped in Array and element is instance of String.
I would recommend Array.prototype.filter and String.prototype.startsWith();
let data = [
'600030/01/2018/163904',
'600010/01/2014/3789',
'600030/01/2020/47104',
'600030/01/2012/39104',
'600010/01/2011/93817',
];
let filteredData = data.filter((d)=>!d.startsWith('600030'));
console.log( filteredData );
Result is
[
'600010/01/2014/3789',
'600010/01/2011/93817'
]

Remove non-numeric items in a listu using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am working with an array in Javascript that contains several IDs in them but I would like to filter out all non-numeric entries using regex and return that array of just numbers. For example, I have myArray = ['131125150138677','CI%20UW%20SYSTEMS%20S','040964100010832'] where I want to get rid of the second item in the list since it's non-numeric.
So use Filter and test to see if they are numbers
var myArray = ['131125150138677', 'CI%20UW%20SYSTEMS%20S', '040964100010832']
var filtered = myArray.filter(Number)
console.log(filtered)
var filtered2 = myArray.filter(s => s.match(/^\d+$/))
console.log(filtered2)

Why does JSON.parse() add numbers in front of elements? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a string where I build my build my "json-like" format, like
_toBeFormated =
[
{"foor":"bar","foo":"bar","foo":["bar,bar"]},
{"foor":"bar","foo":"bar","foo":["bar,bar"]},
{"foor":"bar","foo":"bar","foo":["bar,bar"]}
]
But after calling JSON.parse like _afterFormat = JSON.parse(_toBeFormated), my structure looks like the following:
_afterFormat =
0:{"foor":"bar","foo":"bar","foo":["bar,bar"]},
1:{"foor":"bar","foo":"bar","foo":["bar,bar"]},
2:{"foor":"bar","foo":"bar","foo":["bar,bar"]}
If I try to change to JSON Format at the beginning, like leaving out [ ], if failes to parse, also it looks like valid JSON to me. What am I missing, or why does it add the numbers at the beginning?
It doesn't add numbers. The data structure is an array. The tool you are using to look at the array is showing the index of each entry.

Categories

Resources