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);
Related
This question already has answers here:
How can I convert a comma-separated string to an array?
(19 answers)
Closed 1 year ago.
i have this
string = "Art,fitness"
and i want a array like this
[[Art], [Fitnnes]]
if i do string.spli(',')
i got ["Art", "Fitnnes"]
And is not the output i need, also i try
JSON.parse("[" + string.replace(/'/g, '"') + "]");
but dont work and give me [ 'Art,Fitnnes' ];
i need to do a map before the split to create a new array or there are a simple way to do this
You can do it like this
const string = "Art,fitness"
const result = string.split(",").map(item => [item])
console.log(result)
First, we split() the string on the ,, then we map through the outcome array and return the item in another array.
The below code works well
const string = "Art,fitness";
const newArray = string.split(',').map(str=>[str]);
const string = "Art, fitness";
console.log(string.split(',').map(a => [a]));
Loop over the split values with the .map operator and return them as array.
Instead of splitting the list and mapping each item to an array containing one item, you could alternatively manipulate the input string by converting it to JSON and then parsing the JSON as a two-dimensional array.
First, process all tokens that are a sequence of non-delimiters (ignoring white-space) and wrap them in quotes and brackets (inner-array).
Next, surround the string with brackets (outer-array).
Finally, you can parse the string as JSON data.
const strListToMatrix = listStr =>
JSON.parse(
listStr
.replace(/\s*([^,]+)\s*/g, '["$1"]')
.replace(/(.+)/, '[$1]'));
console.log(strListToMatrix('Art,fitness'));
.as-console-wrapper { top: 0; max-height: 100% !important; }
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 passing myself a string of results from php by ajax that I would like to put into a two dimensional array in JavaScript
The string looks like: value1^*value2^*value3^*value4***value1^*value2^*value3^*value4
I would like to split the values by '^*' into the first row of the dimensional array, then the next row would be after the '***'
Desired array:
var Text = [['value1', 'value2','value3','value4'],[value1','value2','value3','value4']];
You can use split() to split your string into an array of strings ( value1^*value2^*value3^*value4 and value1^*value2^*value3^*value4 ), after that you will need map() to creates a new arrays inside each array which we get before.
Example:
var str = "value1^*value2^*value3^*value4***value1^*value2^*value3^*value4"
str = str.split('***')
str = str.map((value) => value.split('^*'))
console.log(str)
You can do something like that
var input = "value1^*value2^*value3^*value4***value5^*value6^*value7^*value8";
var res = input.split('***').map(function(rowValues){
return rowValues.split('^*');
})
console.log(res);
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);