Print the name from JSON object to Java script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2024 10:01 PM
Hello all,
In these below lines, I want to print the title of the book, where the availability is available.
How can I write the code in java script for this JSON format?
Please Note:
The script should be dynamic like, in future if we add more books, then it should print those also.
{
"library": {
"name": "City Library",
"location": "123 Library St, Booker town",
"books": [
{
"title": "The Game of Shadows",
"author": "James Bob",
"availability": "Available"
},
{
"title": "Invincible",
"author": "William carl",
"availability": "Checked Out"
}
]
}
}
Please help.
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2024 10:06 PM
@Lucky1 You can loop through the array of books and print the title of each book that has the availability status set to "Available". If new books are added to the books array in the future, this script will automatically include them in the output.
// Sample JSON data
var libraryData = {
"library": {
"name": "City Library",
"location": "123 Library St, Booker town",
"books": [
{
"title": "The Game of Shadows",
"author": "James Bob",
"availability": "Available"
},
{
"title": "Invincible",
"author": "William carl",
"availability": "Checked Out"
}
]
}
};
// Function to print book titles that are available
function printAvailableBooks(library) {
// Ensure the library has a books array
if (library && library.books && Array.isArray(library.books)) {
// Loop through all books in the library
library.books.forEach(function(book) {
// Check if the book is available
if (book.availability === "Available") {
console.log(book.title);
}
});
} else {
console.error("No books array found or incorrect library structure");
}
}
// Call the function with the library data
printAvailableBooks(libraryData.library);
Hope this will help you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2024 10:43 PM
Hello Abhay,
Thanks for your response.
I did not understand these lines. Can you please explain me in detail.
if (library && library.books && Array.isArray(library.books)) { // What is here Array.isArray(library.books)
library.books.forEach(function(book) { //forEach is like for loop I understood. but what is that function(book) ?
Thanks in advance.
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2024 05:04 AM
Hi Lucky1,
I think would be best for you to lookup a Javascript training course and invest some time into it. There are several paid versions out there (udermy comes to mind), but w3schools has a free one (that I haven't tried).
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2024 10:06 AM
The above works in Scripts - Background if you change line 29 to "gs.info(book.title);"