I have a object which I am getting from database
Note: The below array is the output of console.log(ansoptions);
[{id:1, option:"Yes"},{id:2, option:"No"},{id:3, option:"Other"}]
This initially is in datatype string. I want to convert it into array of objects and loop to get id and option values.
I have tried below code
var ansoptions = JSON.parse(JSON.stringify(props.answerOptions));
console.log(ansoptions);
Array.from(ansoptions, item => {
console.log(item);
})
For the console.log(item) I am getting the output as weird as below
[
{
i
d
:
1
,
o
p
and so on.....
How do I get it? Please help !!
Parse the JSON (a string), then just loop over the array that's created.
const json = '[{"id":1, "option":"Yes"},{"id":2, "option":"No"},{"id":3, "option":"Other"}]';
const arr = JSON.parse(json);
for (let obj of arr) {
console.log(obj.id, obj.option);
}
var objdata = $.parseJSON(data.d);
//here i am parsing json i have six rows in database (Microsoft SQL server 2008)
So my code is creating separate json object for each row so there is total six object in my variable var objdata how to store objdata in array variable?
You could use .map() method to map the object keys to an array.
var objdata = $.parseJSON(data.d);
var arr = Object.keys(objdata).map(function(k) { return objdata[k] });
Another approach would be to use the Object.entries() method, wich wil return an array containing a [key, value] arrays for each property of the given object.
var objdata = $.parseJSON(data.d);
var arr = Object.entries(objdata);
Or simply use the Object.values() method to get the object properties as an array. (This may not work in some browsers)
var objdata = $.parseJSON(data.d);
var arr = Object.values(objdata);
I have JavaScript code below.
var data = "[{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}]";
I need convert the string to an data by delete the "" surrounding the array stored as "data" in JavaScript so after convert it suppose like below:
var data = [{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}];
How to make the convert?
To convert a JSON object to Javascript object use:
var data = '[{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}]';
JSON.parse(data);
But first change the double quote to single quote, otherwise the JSON object wont be a valid JSON.
After this you can walk the array in the following way:
var jsonParsed = JSON.parse(data);
for(var val in jsonParsed) {
if(jsonParsed.hasOwnProperty(val)) {
// do something with the values
}
}
I'm stringyfing an object like:
"{'foo': 'bar','task':[{'task1':'task1'}]}"
How can I turn the string back to an object?
Try the following solution to parse the stringified JSONObject.
// Stringified JSON Object
var stringifiedJSONObject = '{'foo': 'bar','task':[{'task1':'task1'}]}';
// Parsing string object to json
var jsonObject = JSON.parse(stringifiedJSONObject);
// Get the inner array. The below object is a JSON Array of Objects
var innerArray = jsonObject.task;
// displays the value of task1
alert(innerArray[0].task1);
I am making a ajax request in jquery and in return getting the response but not as an array.
{"ErrorCode":0,"SeriesSocialStats":{"8970471":{"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"}}}
{"ErrorCode":0,"SeriesSocialStats":{"184072":{"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}}}
I want to merge the above two response and create an array something like this :
{"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"},{"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}
Please suggest how to do it. I want to take it in array and store it locally may be in config varaible get:[] and then access somewhat like config.get[data["seriesId"]].
you need to convert your response into an array of objects:
var response = [
{"ErrorCode":0,...},
{"ErrorCode":0,...},
{"ErrorCode":0,...},
]
in actual:
jsonResponse = [
{"ErrorCode":0,"SeriesSocialStats":{"8970471":{"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"}}},
{"ErrorCode":0,"SeriesSocialStats":{"184072":{"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}}}
]
then loop through:
var newArray = []
for(var i=0;i<jsonResponse.length;i++){ //loop through items
var stats = jsonResponse[i].SeriesSocialStats;
for(key in stats){ //loop through "SeriesSocialStats" numbers
newArray.push(stats[key]);
}
}
so it will be like:
newArray = [
{"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"},
{"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}
]
You could do
var obj1 = {"ErrorCode":0,"SeriesSocialStats":{"8970471":{"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"}}};
var obj2 = {"ErrorCode":0,"SeriesSocialStats":{"184072":{"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}}};
var arr = [];
arr.push(ob1.SeriesSocialStats);
arr.push(ob2.SeriesSocialStats);
Best way convert your server response to array structure, like mentioned by Joseph, instead of doing double processing from object to array.