Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6420

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T06:22:06+00:00 2024-11-27T06:22:06+00:00

JavaScript map Function with JSON

  • 60k

The map() method in JavaScript is used to create a new array populated with the results of calling a provided function on every element in the calling array. It's one of the most commonly used array methods due to its utility in transforming data in a functional programming style.
Creates a New Array: map() does not modify the original array. Instead,
it creates a new array with the transformed values.

Syntax

array.map(function(element, index, array) {   // Return transformed element here })  
Enter fullscreen mode Exit fullscreen mode

*Example *

Displaying JSON Data in a Card Layout using HTML, CSS, and JavaScript

Let's display the given JSON data in a card layout on an HTML page using the map function in JavaScript. We will use HTML for the structure, CSS for styling, and JavaScript to fetch and display the data.

*HTML File *index.html

<!DOCTYPE html> <html lang="en">   <head>     <meta charset="UTF-8" />     <meta name="viewport" content="width=device-width, initial-scale=1.0" />     <link       href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"       rel="stylesheet"       integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"       crossorigin="anonymous"     />     <script       src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"       integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"       crossorigin="anonymous"     ></script>     <title>JSON Data Display</title>     <style>       table {         width: 100%;         border-collapse: collapse;       }       th,       td {         padding: 10px;         text-align: left;         border: 1px solid #ddd;       }       th {         background-color: #f2f2f2;       }     </style>   </head>   <body>     <div class="container">       <div class="row text-center mt-4 mb-2">         <h1>Data from JSON</h1>         <p>Understanding the JavaScript map Function with JSON</p>       </div>       <div id="data-container"></div>     </div>      <script src="script.js"></script>   </body> </html>  
Enter fullscreen mode Exit fullscreen mode

*JavaScript File *script.js

const data = [   {     id: 1,     name: "Sudhanshu Gaikwad",     age: 23,     country: "India",   },   {     id: 2,     name: "Jane Smith",     age: 30,     country: "India",   },   {     id: 3,     name: "Michael Johnson",     age: 35,     country: "India",   }, ];  document.addEventListener("DOMContentLoaded", () => {   const container = document.getElementById("data-container");    const table = document.createElement("table");    const thead = document.createElement("thead");   const headerRow = document.createElement("tr");    const headers = ["ID", "Name", "Age", "Country"];   headers.map((headerText) => {     const th = document.createElement("th");     th.textContent = headerText;     headerRow.appendChild(th);   });    thead.appendChild(headerRow);   table.appendChild(thead);    const tbody = document.createElement("tbody");   // Heare We have used Map() Function For the all data Displayed      data.map((item) => {     const row = document.createElement("tr");      Object.values(item).map((value) => {       const td = document.createElement("td");       td.textContent = value;       row.appendChild(td);     });      tbody.appendChild(row);   });      table.appendChild(tbody);   container.appendChild(table); });  
Enter fullscreen mode Exit fullscreen mode

Output

Image description

beginnersjavascriptprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.