How to convert String into Array in javaScript - javascript

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);

Related

Turn a String to an array Declaration

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]]

Loading string to array of array

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);

Regex extract contents

So, I have the following data here:
{"screenName":"fubars","msgHash":"C5STUYqhjzNiP6LLVbPlTF3zYLVYXHrm","imgURL":null,"userColor":"#00a4a0","messageTime":"2:50 PM","messageDate":1442256635621,"accountType":"m","accountTypeID":"z2ZkdXqck-JO45hqXVXH","isModerator":"","badges":""
I've written some regex to extract strings, but if I search for example "screenName" it gets the "fubars" part and the rest of the string, I only want the "fubars" part...
code:
function extractSummary(iCalContent, what) {
eval("var rxm = /\""+what+"\": \"(.*)\"/g");
console.log(rxm);
setTimeout(function(){},1500);
var arr = rxm.exec(iCalContent);
return arr[1];
}
If you have some data in the form of a JSON string, you can use JSON.parse to convert it into a JSON object. You can then use dot or index property getters.
var jsonString = '{"screenName":"fubars","msgHash":"C5STUYqhjzNiP6LLVbPlTF3zYLVYXHrm"}';
var jsonObject = JSON.parse(jsonString);
console.log(jsonObject.screenName);
console.log(jsonObject['screenName']);
There's no need to use regexes here.

Parse JSON array from string

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);

How to get from a string array?

an array of server:
Array ( [28.01.2015] => Array ( [03] => 2 [02] => 4 ) )
was converted into a string using the json_encode.
The result was a string:
{"28.01.2015":{"03":2,"02":4}}
How to use Javascript to convert this string into an array ?
You can turn that into a JavaScript object by using JSON.parse():
var my_object = JSON.parse('{"28.01.2015":{"03":2,"02":4}}');
JSON.parse('{"28.01.2015":{"03":2,"02":4}}');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
For achieve this, you need use JSON.parse.
JSON.parse('{"28.01.2015":{"03":2,"02":4}}');
Or you could organize better:
var string = '{"28.01.2015":{"03":2,"02":4}}';
var object = JSON.parse(string);

Categories