In JS :
I have this string ="[36.79025,3.01642],[36.71477,2.99761]";
I want it to be turned To a real Array =[[36.79025,3.01642],[36.71477,2.99761]];
Is this possible?
var string = "[36.79025,3.01642],[36.71477,2.99761]";
var arr = JSON.parse(`[${string}]`);
console.log(arr);
[[36.79025,3.01642],[36.71477,2.99761]]
Related
I have a string like "[1,2,3]" and I want to convert it into array like [1,2,3] using JavaScript. Can anyone help me to do this?
Since the string you want to convert is compatible to the JSON format (JavaScript object notation), you can use JSON.parse to convert it into an array:
const str = "[1,2,3]";
const arr = JSON.parse(str);
console.log(arr);
Here you go
var dat = "[1,2,3]";
var myData = JSON.parse(dat);
console.log(myData);
I am getting a set of arrays in string format which looks like
[49,16,135],[51,16,140],[50,18,150]
Now I need to save them in an array of arrays. I tried it like
let array = [];
let str = '[49,16,135],[51,16,140],[50,18,150]';
array = str.split('[]');
console.log(array);
but it is creating only one array including all string as an element while I need to have
array = [[49,16,135],[51,16,140],[50,18,150]]
Add array delimiters to each end of the string, then use JSON.parse:
const str = '[49,16,135],[51,16,140],[50,18,150]';
const json = '[' + str + ']';
const array = JSON.parse(json);
console.log(array);
You are splitting it incorrectly, in the example, it will only split of there is a [] in the string
You can create a valid JSON syntax and parse it instead like so,
let str = '[49,16,135],[51,16,140],[50,18,150]';
let array = JSON.parse(`[${str}]`);
console.log(array);
Another way you could achieve this is by using a Function constructor. This method allows you to "loosely" pass your array.
const strArr = "[49,16,135],[51,16,140],[50,18,150]",
arr = Function(`return [${strArr}]`)();
console.log(arr);
I'm trying to create an array from a string that is separated into different words, each of which would be a separate element. The split() works fine here, however, the string is stored as a list in the array. How do I store a string into an array such that each word will be its own element in the array and not a list?
var myArray = [];
var string = "Hello, my name is Cameron and I like turtles."
myArray.push(input.split(" "));
console.log(myArray.indexOf('name')); //-1
console.log((myArray[0])); //prints string in list
console.log((myArray[1])); //undefined
console.log((myArray[2])); //undefined
this should work :
var string = "Hello, my name is Cameron and I like turtles."
var myArray = string.split(" ");
You are pushing an array into another array. You can initialize the value with the splitted string directly.
var string = "Hello, my name is Cameron and I like turtles.",
myArray = string.split(" ");
<script>
var string = "Hello, my name is Cameron and I like turtles.";
var toArray = string.split("");
console.log(toArray();//Will Display string as array
</script>
Here you go
So you've created an array and string, that's fine.
var myArray = [];
var string = "Hello, my name is Cameron and I like turtles."
Now you're splitting the string and pushing the result of that to your first array.
myArray.push(input.split(" "));
This is where I think you're going wrong. You need to add the results to your original array, however you are adding them all as one item and in turn creating a multidimensional array.
You can either override your array:
myArray = input.split(" ")
Or concat them. This might be a better approach if you need to do it multiple times.
myArray.concat(input.split(" "));
I want to convert the following string to an array
var string = '["YES","NO"]';
How do I do this?
use the global JSON.parse method
JSON.parse('["YES","NO"]'); // returns ["YES", "NO"]
You can also use the JSON.stringify method to write the array back to a string if thats how you are storing it.
JSON.stringify(["YES", "NO"]); // returns '["YES", "NO"]'
var str= '["YES","NO"]';
var replace= str.replace(/[\[\]]/g,'');
var array = replace.split(',');
Fiddle : http://jsfiddle.net/9amstq41/
You can also use $.parseJSON:
var string = '["YES","NO"]';
var array = $.parseJSON(string);
I want to extract all JSON objects from a string randomly containing them and add them to an array.
Sample string:
"I was with {"name":"John"}{"name":"Anne"}{"name":"Daniel"} yesterday"`
how can i extract the JSON objects from this sample string?
One approach to this is to use the str.search(regexp) function to find all parts of it that fulfill the JSON regex found here. So you could write a function that searches over the string for regexp matches.
To actually extract the object from the string you could use these two functions for the regex and then loop over the string until all objects have been found.
var match = str.match(regex);
var firstIndex = str.indexOf(match[0]);
var lastIndex = str.lastIndexOf(match[match.length-1]);
var JSONobjects = [];
while( str.match(regex){
//extract the wanted part.
jsonObject = substr(str.indexOf(match[0],str.lastIndexOf(match[match.length1-]));
//remove the already found json part from the string and continue
str.splice(str.indexOf(match[0],str.indexOf(match[0] + jsonObject.length());
//parse the JSON object and add it to an array.
JSONobjects.push(JSON.parse(jsonObject));
}
var a = JSON.parse('{"name":"John"}');
a ==> Object {name: "John"}
var b = [];
b.push(a);