Arrange data in JSON file based on job title (role name) category

My current code successfully outputs data from a JSON file, but I'm looking to enhance it by organizing the output based on the "Role Name". For example, individuals with the role of Associate Editor should have their information displayed in one section, while those with the role of Editor and Chief are placed in another. Similarly, members of the Editorial Advisory Board should be grouped together. Below is my HTML structure, followed by the JavaScript code. To begin, I have included some sample JSON data.

Example Json Data:

[
  {
    "Journal: Journal Name": "Accounts of Chemical Research",
    "Role Name": "Editorial Advisory Board",
    "Gender": "Female",
    "Full Name": "Joanna Aizenberg",
    ...
  },
  {
    "Journal: Journal Name": "Accounts of Chemical Research",
    "Role Name": "Editorial Advisory Board",
    "Gender": "Male",
    "Full Name": "Ayyappanpillai Ajayaghosh",
    ...  
  },
  {
    ...
  }
]


<html>
...
</head>
    

HTML Structure:

<body>
...
 

Javascript Code:

$(document).ready(function() {
    
        
    
    
    $.getJSON("jci-test-file.json", function(dataJCI){
        console.log("hell0");
        //var journalName =dataJCI[i]["Journal: Journal Name"];
            //var fullName= dataJCI[i]["Full Name"];
            //var int=dataJCI[i]["Institution"];
              //$("#editorList").append("<h2>" + journalName + "</h2>").append("<h3>" + fullName + "</h3>").append ("<h4>" + int + "</h4>");
      

  
        
        editorList(dataJCI);
        
    });
    
    
});

...



function displayContent(journalList){
    for(i in journalList){
       //var journalName =journalList[i]["Journal: Journal Name"];
       var fullName= journalList[i]["Full Name"];
       var int=journalList[i]["Institution"];  
        $("#editorList").append("<h3>" + fullName + "</h3>").append ("<h4>" + int + "</h4>"); 
     
        
    }
}

    

Answer №1

I have personally verified that this solution is effective with your specific dataset.

data.reduce((a,c) => {
  let role
  if(role = a.find(_a => _a["Role Name"] === c["Role Name"]))
    role.persons.push(c)
  else
    a = [
      ...a,
      { "Role Name": c["Role Name"], persons: [c] }
    ]
  return a 
},[])
[{…}]
  0:
    Role Name: "Editorial Advisory Board"
    persons: Array(3)
      0: {Journal: Journal Name: "Accounts of Chemical Research", Role Name: "Editorial Advisory Board", Gender: "Female", Full Name: "Joanna Aizenberg", Last Name: "Aizenberg", …}
      1: {Journal: Journal Name: "Accounts of Chemical Research", Role Name: "Editorial Advisory Board", Gender: "Male", Full Name: "Ayyappanpillai Ajayaghosh", Last Name: "Ajayaghosh", …}
      2: {Journal: Journal Name: "Accounts of Chemical Research", Role Name: "Editorial Advisory Board", Gender: "Male", Full Name: "Scott L. Anderson", Last Name: "Anderson", …}
      length: 3

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating the ContextKey of the DynamicPopulateExtender based on the selected value from a DropDownList dynamically

I am facing an issue with my DynamicPopulateExtender control. I need it to render HTML based on the value of an asp:DropDownList. The problem lies in writing the JavaScript code that can fetch the dropdown value, assign it to the DynamicPopulate control&ap ...

Unable to extract query parameters from URL using Express JS as req.query returns an empty object

I came across discussions about this issue here and here, but unfortunately, the solutions provided didn't work for me. I'm attempting to extract parameters from the URL using req.query. In my server.js file, I've implemented the following: ...

Retrieving JSON data to create and showcase an HTML table

Can you help me figure out what's going wrong with my code? I have an HTML page with a table where I fetch data from the web in JSON format using JavaScript. The logic works perfectly when the fetch code is let to run on its own, but when I try to ex ...

Managing numerous API requests in React Native

As I work on implementing a search field, I've encountered a challenge. Whenever a user enters text in the search field, a timer resets to 300 ms before an API call is sent to fetch autocomplete results. After receiving these results, the app then wai ...

JavaScript code in AJAX response functions properly in browsers like Firefox, Chrome, and Opera. However, it encounters issues in Internet Explorer 11, displaying an error message stating

After searching through various posts, I was unable to find a solution to my question. My query involves requesting a jQuery Datepicker via AJAX. I have provided an example for you to review in Firefox, Chrome or Opera: Ajax javascript example Unfortuna ...

Add the item to an array to store its state

I have a state variable that is initially set as an empty array const [boxes, setBoxes] = useState([]); const [showAddGalley,setShowAddGalley]=useState({galleyNo:""}); I created a function to handle form submissions, where I want to update the b ...

Combining two arrays filled with objects to form a collection of Objects on a Map

In my JavaScript code, I'm receiving data from a WebService that looks like this: { "fire": { "totalOccurence": 2, "statsByCustomer": [ { "idCustomer": 1, "occurence": 1 }, { "idCustomer": 2, ...

Setting up a function in React to utilize an image stored in state

I have a function in my react component for image classification that retrieves the image from the img tag using document.getElementById: const img = document.getElementById('animal_image');. The image uploaded via the file input updates the sta ...

Retrieve targeted information from a JSON document

I’m currently immersed in a project that requires knowledge of Javascript/Jquery/Ajax, areas I’m still trying to get the hang of. Unfortunately, there’s no one around me to turn to for help. The task at hand involves making an Ajax call to fetch an ...

Exploring Grails Assets, Redirections, Secure Sockets Layer, and Chrome

Using Grails 2.1.1 with the resources plugin, I have encountered an issue when incorporating the jstree library which comes with themes configuration: "themes":{ "theme":"default", "dots":false, "icons":true } The JavaScript in the library locat ...

Strange error message: Attempting to set properties of null (changing 'innerHTML')

I have been working on a project where I am creating a website that retrieves data from a specified URL, displays it on the front end, and performs certain functionalities with that data (although this part is not yet implemented). However, I have encounte ...

"Discover the steps to seamlessly integrating Snappuzzle with jQuery on your

I am a beginner when it comes to javascript and jquery, and I recently came across the snappuzzle plugin which caught my interest. After visiting snappuzzle plugin, I decided to download and link jQuery, jQuery UI, and the snappuzle.js in my HTML file. I a ...

Is it possible to dynamically populate a dependent select box using Jinja variables?

I am currently using Flask with Jinja2 templates, and I need assistance in creating dependent select boxes. How can I achieve this using Jinja2 or a combination of JavaScript and Jinja2 variables? For example: On the Python side: @app.route('/&apos ...

text box with an immobile header

As the browser window size decreases, the layout changes. However, when scrolling down, the search text box moves up and is no longer visible due to its lack of fixation. How can I make the search text box stay fixed as I scroll down? I tried implementing ...

Dynamically change the state based on intricate layers of objects and arrays

Here is the issue I am facing I am attempting to update the state of an object named singleCarers I have the index of the roster in the rosters array that I need to update However, the key monday: needs to be dynamic, as well as the keys start: and fini ...

Tips for augmenting cell with additional data in react-datepicker

Is it possible to include additional text in a cell using react-datepicker? For example, similar to what is shown in this image: view image here ...

Tips for getting JavaScript to identify a context_dict object from views.py in Django

Recently, I inherited a Django project from a former colleague and now I need to make some changes to the code in order to add a new line to a Google Viz line chart. As the line chart already exists, my approach is to closely follow the logic used by my p ...

What is the best way to adjust and filter an array based on a single value?

Here is an array that needs to be modified: [ {name: "test", value: "test", group: 0}, {name: "test1", value: "test2", group: 0}, {name: "test3", value: "test3", group: 1}, {name: "te ...

Steps to load a script when the document is ready:

What is the best method to include JavaScript using the following code: <script type="text/javascript" src="http://uads.ir/l.php?s=125125&w=5307cd5c027373e1773c9869"></script> only after the page has fully loaded? $(document).ready(funct ...

Instant Pay Now Option for Your WordPress Website with PayFast Integration

I have encountered an interesting challenge that I would like some guidance on. My goal is to integrate a PayFast "Pay Now" button into a Wordpress.com blog, specifically within a sidebar text widget. The tricky part is that I need the customer to input th ...