JSON object to Array Javascript [duplicate] - javascript

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 2 years ago.
Im seeing a lot of answers to this in simple arrays but I need the keys and value. I have a json object but need an array. The object looks like this:
{"Food":"Starbucks", "Job":"Electrician"}
I just need a simple array like this:
["Food" => "Starbucks", "Job" => "Electrician"]
Edit: Im not sure how to type it out. Just need to be able to do an array.map and get the keys and values in javascript.
Here is the final code that I am trying:
const details = jsonObject;
{details.map(function(item,idx){
return<DetailCell>
<Label>{idx}</Label>
<Text style={TextStyle}>{item}</Text>
</DetailCell>
})}
Label should be the key and the Text should be the value.

I think you need this :
[{"Food" : "Starbucks"}, {"Job" : "Electrician"}]

To declare a literal array value is something like this in JSON in node.
{JSONVarWithArrayValue : [0,0,0,0]}

Related

How do I get an object id from JSON format in Javascript [duplicate]

This question already has answers here:
Getting JavaScript object key list
(19 answers)
Closed 1 year ago.
I am using the Alpha Vantage API to get stock market data. The data is returned in this format:
For charting purposes, I need to create an array or object of all of the dates. They are not within the actual data objects so I'm not sure how to do this.
For example, for this specific data I would want an array that looks something like this:
['2021-04-26', '2021-04-26', '2021-04-26', '2021-04-26', '2021-04-26', '2021-05-03'...]
Thanks for the help!
You can use Object.keys().
You didn't provide sample json, so this is formatted code and not a snippet.
const dates = Object.keys(data['Time Series (Daily)'])

How to access object of an object returned by json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
in a little laravel application I'm working on I'm returning data from an ajax request like this:
return response ()->json ( $subject::where('id', $subject->id)->with('division')->get(['id', 'name']));
This returned something quite like an object that have nested objects. This how my results looks when I log it to the console.
I want to get the name and id of the subject details returned, which in this case is History and 8. Also I want to be able to access the division array and properties of the object it has.
I do this to log the name of the subject console.log(data.name) but in returned I get:
undefined
How can I achieve this?
You have an array of objects, which has one property (division) - which contains another array. So you have to access the array indices
console.log(data[0].division[0].name);
Looking at the object structure we see that this is an array of elements, so it should be data[0].name to fetch History text
I'm assuming data is your entire object.
Try data[0].name

Javascript Turn string into array [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 7 years ago.
I want to turn this into an array
"["65747add-afd2-45b5-92e0-150bbe40e6d9", "9c247ea5-6b81-4f47-a50c-42367dedd50b", "c1555363-aca9-4e04-8844-e0180397c72e"]"
I am getting it from the page like this:
$('#layout-uuids').text()
Is there away to just get it as an array or do I need to turn the string into an array somehow?
Thanks!
That string looks like JSON. If it is indeed JSON, all you need is JSON.parse():
var someArray = JSON.parse($('#layout-uuids').text());

How to correctly get JSON value? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I have this json array
var currencyformats =
{"USD":[
{'symbol':'$', 'left':true}
],
"UAH":[
{'symbol':'₴', 'left':true}
],
"EUR":[
{'symbol':'€', 'left':false}
]
};
How retreive '₴' ?
I tried this (in cookie "to" I've "UAH")
currencyformats[$.cookie("to")].symbol
but I've obtained undefined
The problem is that under each country code, you've defined an array with a single object. That means that after you access the country code, you'll need to also access the first index in the array.
So, assuming the cookie is set to the value that you're expecting:
currencyformats[$.cookie("to")][0].symbol;

Convert JSON string to Javascript array [duplicate]

This question already has answers here:
Convert a multidimensional javascript array to JSON?
(9 answers)
Closed 2 years ago.
I have an array of arrays which I have planted in the DOM, so that I can reuse it at a later stage to transfer to the server:
[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]
If I would like to convert it back into a Javascript array, how would I go about doing this?
var obj = $.parseJSON('[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]')
Assuming jquery is ok to use because of tag.
If the browzer has the JSON object then
JSON.parse(string);
or if you have jQuery
$.parseJSON(string);
var array = JSON.parse(my_JSON)
jQuery.parseJSON()
var myArr = $.parseJSON(myJsonString);

Categories

Resources