I'm working with this page: ITEM INFO
and I turn the page into a string:
var info = document.getElementsByTagName("pre")[0].innerHTML;
The entire page is already in the format of an array, so is there a conversion function that will convert it from the string into an array?
Use JSON.parse(info) to parse JSON text into Javascript Array.
var info = document.getElementsByTagName("pre")[0].innerHTML;
var results = JSON.parse(info);
Now in results variable will be an array of parsed JavaScript objects.
Related
I have a String value as input that contains a 2-dimensional array in the following format:
"[[1,0,1,1],[0,1,0,1],[1,0,1,1],[0,1,0,1]]"
Also, I need to build the actual multidimensional array and store it in a new variable.
var arr = [[1,0,1,1],[0,1,0,1],[1,0,1,1],[0,1,0,1]];
I know I can traverse the String or even use Regular Expressions to build the array. But I wonder if there is any function similar to eval() in Python to convert the String to an equivalent JS array object directly (despite being a slow process).
var arr = eval("[[1,0,1,1],[0,1,0,1],[1,0,1,1],[0,1,0,1]]");
Considering you have it stored like this
let x = "[[1,0,1,1],[0,1,0,1],[1,0,1,1],[0,1,0,1]]"
let arr = JSON.parse(x)
You array is a valid json that can be parsed and manipulated
let x = "[[1,0,1,1],[0,1,0,1],[1,0,1,1],[0,1,0,1]]"
let arr = JSON.parse(x);
console.log(arr)
Beware that if the string is not a valid json the above code will fail
I have a response from the server that is like this:
users: ["beautykarttworld","sbdanwar","haiirfavs","show__tanz","kinjal_suhagiya_9999","_adultmafia_"]
It's a string not an array like it appear, since I need an array to work on, how I can remove the square backets and then the commas to obtain a normal array?
I've tried in this way:
var data = ["beautykarttworld","sbdanwar","haiirfavs","show__tanz","kinjal_suhagiya_9999","_adultmafia_"]
data.replace('[', '').replace(']', '').split(',');
but chaining two .replace() functions and a split() isn't the best solution. Can anyone halp me?
In your example data is an array.
However, if data was a string you would convert it to an array like this:
var arr = JSON.parse(data);
In short,
Here you have provided data in normal string formate not in JSON string formate
var data = 'users: ["beautykarttworld","sbdanwar","haiirfavs","show__tanz","kinjal_suhagiya_9999","_adultmafia_"]'
var squareBracketData = data.substr(data.indexOf("["))
var array = JSON.parse(squareBracketData)
console.log(array)
Some personal advice, Try to send JSON stringify data from the
server so it will make your life easy
Example:
var users =["beautykarttworld","sbdanwar","haiirfavs","show__tanz","kinjal_suhagiya_9999","_adultmafia_"]
// Stringify data
var data = JSON.stringify({users})
console.log("Data")
console.log(data)
// retrieve data from string
var parsedData = JSON.parse(data)
var parsedUsers = parsedData.users
console.log("parsedUsers")
console.log(parsedUsers)
i got a string which was actually an array as I got it from database.
0
when i add it to an object, it becomes a string with quotation mark "" , but i want it to be array, what should I do?
e.g.
temp = [{"abc"=123},{"bcd"="234},...] //this is actually a string
helloObj = {items:temp};
consol.log(helloObj); //will become items: "[{"abc"=123},{"bcd"="234},...]"
//I want it become items: [{"abc"=123},{"bcd"="234},...]
//I have tried below, not work:
temp = [temp];
helloObj = {items:temp};
consol.log(helloObj); //will become items: ["[{"abc"=123},{"bcd"="234},...]"
]
What you have is a JSON string. You need to parse it from the JSON notation which will convert it to an object (array in this case).
var aray = JSON.parse('[{"abc"=123},{"bcd"="234},...]');
console.log(aray);
So you're getting JSON back from your database. Simply run JSON.parse(str) where str is your string and it will convert it back to an array.
I am passing data back to java script through a rest api. The rest API return data in the following below mentioned structure.I am confused what format of json is it and how can i iterate on the array inside and get the int value individually.
The format of my response is like :
{"searchOrder":[
{"location":"10","trackingId":"656"},
{"location":"34","trackingId":"324"},....],
"count":100}
i want searchOrder as array of json to display in table and count to show total row.How can i get them?
Just iterate over the array and extract the values
// Turn the JSON into a JS object (maybe in your XHR handler)
var data = JSON.parse(jsonString); // jsonString could be responseText from XHR
var arrayOfTrackingIdValues = data.searchOrder.map(function (value) {
return value.trackingId;
});
JavaScript provide built-in function JSON.parse() to convert the string into a JavaScript object and drive/iterate the each element based on type obj/array
var obj = JSON.parse(text);
I am using the following script to help me convert javascript arrays to json strings: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
How come this works:
var data = [];
data[1] = [];
data[1].push('some info');
data[1].push('some more info');
json_data = JSON.stringify(data);
alert(json_data);
And this does not (returns a blank):
var data = [];
data['abc'] = [];
data['abc'].push('some info');
data['abc'].push('some more info');
json_data = JSON.stringify(data);
alert(json_data);
I want to convert multi-dimensional javascript arrays, but it seems I cannot use stringify() if I name my array keys?
JSON arrays are integer-indexed only.
You can change your first line to use {} as in http://jsfiddle.net/5YXNk/, which is the best you can do here.
Check the array syntax at http://json.org/ -- note arrays contain values only, which will be implicitly indexed by non-negative integers. That's just the way it is.
There is no such thing as an associative array in Javascript. You're going to have to use an object if you want to use string "keys".