process json array object [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
I am new to javascript(json) I want to process each json object,the example prototype is given below please help me to solve the problem,I need to console it each room number given in json sample below
how can I loop through the each item in json array so that I can get individual json element and console it on browser window
let data = [
{
"king" :[
{
"id" : 1,
"room_no" : 101,
"price" : 2000
},
{
"id" : 2,
"room_no" : 102,
"price" : 3000
},
]
}
]
sorry for my bad english

Is this what you are looking for?
let data = [{
"king": [{
"id": 1,
"room_no": 101,
"price": 2000
},
{
"id": 2,
"room_no": 102,
"price": 3000
},
]
}]
data[0].king.forEach(item => console.log(item.room_no));

Related

Iterating over multiple nested JSON using Object.keys in JS [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Loop through an array in JavaScript
(46 answers)
Closed 3 years ago.
I have the following data:
const temp = {
"_id" : "5def6486ff8d5a2b1e52fd5b",
"data" : {
"files" : [
{
"path" : "demo1.cpp",
"lines" : [
{
"line" : 18,
"count" : 0
}
]
},
{
"path" : "file2/demo2.cpp",
"lines" : [
{
"line" : 9,
"count" : 0
}
]
}
]
}
}
Now, I want to access the path, and line variable inside each of the files. I am still learning using JS and pretty new with Object.keys.
I have this:
Object.keys(temp.data.files).forEach(file => console.log(file));
This simply prints 0 and 1. Where am I going wrong in this?
This is because temp.data.files is an array. Object.keys is used to loop through Object.
Try this:
const temp = {
"_id": "5def6486ff8d5a2b1e52fd5b",
"data": {
"files": [
{
"path": "demo1.cpp",
"lines": [
{
"line": 18,
"count": 0
}
]
},
{
"path": "file2/demo2.cpp",
"lines": [
{
"line": 9,
"count": 0
}
]
}
]
}
}
temp.data.files.forEach(file => console.log("path ", file.path, " lines ", file.lines));

Store an Array in sessionstorageItem [duplicate]

This question already has answers here:
How to store objects in HTML5 localStorage/sessionStorage
(24 answers)
Closed 7 years ago.
I have the following javascript objects. I would like to store the following array on sessionstorageItem, but it was giving me error. How can I able to store an array in sessionstorageItem?
data=[];
data[0] = [{
"num": 29,
"ser": 1,
}, {
"num": 44,
"ser": 2,
}]
data[1]=[{
"num": 10,
"ser": 3,
}]
allData = data.reduce(function (a, b) { return a.concat(b) });
// the following line gives me an error
var MDData=JSON.parse(sessionStorage.MDData);
if (MDData!==null) {console.log("Hello")}
sessionStorage.setItem('MyData', JSON.stringify(allData));
webStorage can only store strings. So you need to stringify your data.
sessionStorage.setItem('Myata', JSON.stringify(allData));
To retrieve it back you can do:
JSON.parse(sessionStorage.Myata);
you have and extra ')' in "sessionStorage.setItem('Myata', sell.allData));"

Access last JSON element with javascript [duplicate]

This question already has answers here:
Get the last item in an array
(59 answers)
Closed 8 years ago.
Good evening,
I'm facing a simple problem in JSON. I'm developping a smartwatch (Pebble) app which gets data from an API, which returns the following :
{
"name": "toto",
"count": 55,
"results": {
"collection1": [
{
"article": "Content number 1"
},
{
"article": "Content number 2"
},
{
"article": "Content number 3"
}
]
}
}
The problem is the following : is it possible to access only the latest "article" content from this JSON ? So in this case, it would be "Content number 3".
Here is what I've got so far, but it returns only the 1st element :
ajax({ url: 'MY_API_URL', type:'json' }, function(data){
simply.vibe(); // Vibrate the watch
var message = data.results.collection1[0].article;
simply.body(message); // display the message on the watch
Thanks a lot !
That would be:
data.results.collection1[data.results.collection1.length - 1].article
data.results.collection1[data.results.collection1.length-1].article;

iterate json array in javascript [duplicate]

This question already has answers here:
draw google graph from json [closed]
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Parse JSON in JavaScript? [duplicate]
(16 answers)
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 8 years ago.
hi i have generated following json , can i iterate through this
i have validated this josn using some sites
following is just example there can be more section like MATHS, LOGICAL REASONING ,ENGLISH
they can also have their individual types
{ "MATHS": [
{
"Section": "MATHS",
"Type": "INCORRECT",
"Count": "5"
},
{
"Section": "MATHS",
"Type": "NOT SOLVED",
"Count": "20"
}
],
"LOGICAL REASONING": [
{
"Section": "LOGICAL REASONING",
"Type": "CORRECT",
"Count": "1"
},
{
"Section": "LOGICAL REASONING",
"Type": "INCORRECT",
"Count": "4"
},
{
"Section": "LOGICAL REASONING",
"Type": "NOT SOLVED",
"Count": "20"
}
]
}
i have searched on may question on stack overflow but none of them can help me
Your JSON has 2 top-level elements. So, you can't logically iterate across the entire document (without flattening it).
But you can iteration across the "MATHS" and "LOGICAL REASONING" elements.
For example, using underscore:
_(data.MATHS).each(function(item) {
var section = item.SECTION;
var type = item.TYPE;
var count = item.COUNT;
// Now do something with them
});
Note the different access method to the 'LOGICAL REASONING' element, because of the space.
_(data['LOGICAL REASONING').each(function(item) {
var section = item.SECTION;
var type = item.TYPE;
var count = item.COUNT;
// Now do something with them
});
If you wanted all the data together, one way of doing it would be:
var flattened = _.union(data.MATHS, data['LOGICAL REASONING']);
_(flattened).each(function(item) {
// Process...
});
Check out the underscore docs for some more information about the possibilities. Many ways to skin a cat.
http://underscorejs.org/
Cheers.

get value from json when passed key is a variable [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I am creating a list by fetching values from a JSON file. It is a nested JSON and the list items are- "Thriller","Fiction" which are basically keys for the next level.
on click of the item I'm passing its name(i.e. Thriller/fiction) to another function...
var val = thriller.
Now I need to fetch the value(i.e. "book" & bookname) corresponding to the passed key in this new function. I'm not able to do so using dot operator-
data.library.val not working..
If anybody has worked on something similar..please help..
JSON:
{ "library": [
{
"Thriller": [
{ "book": "ABC" },
{ "book": "DEF" }
]
},
{
"Fiction": [
{ "book": "GHI" },
{ "book": "JKL" }
]
},] }
Code snippet:
$.getJSON('resources/abc.json', function(data){
var i = data.library;
$("#menuList1").css('display','block');
$(i).each(function(key, value){
$.each(value, function(key, value){
console.log(key);
$("#menuList1").append(''+key+'');
});
}); });
Use data.library[key]
MDN Documentation
Your Json is not valid, this },] specially. A good version :
{
"library": [{
"Thriller": [{
"book": "ABC"
}, {
"book": "DEF"
}]
}, {
"Fiction": [{
"book": "GHI"
}, {
"book": "JKL"
}]
}]
}
you can refer the website http://jsonlint.com for validating your json.

Categories