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

Tips for handling nested arrays in Logstash JSON processing

I am encountering an issue with nested fields containing arrays within an array in JSON format as shown below: { "foo": { "bar": [ [ "a", "b" ], [ "c", ...

Why won't hover over function properly in separate divs for two items?

My goal is to show text when hovering over a logo, but the text and logo are in different divs. Despite trying various solutions like using display: none and display: block, I still can't get it to work. import styles from '../styles/Float.module ...

Animation that increments to a predetermined value

I'm trying to create a counter animation that dynamically animates a value calculated by the checkboxes selected. The calculation is working fine, but the animation is not happening. http://jsfiddle.net/dbtj93kL/ $('input[type="checkbox"]&apo ...

Using Node.js to retrieve child processes associated with a daemon and terminate them

I am attempting to create a node application that allows me to send the command kill -9 to all child processes of a single daemon. Just to clarify, there is one daemon running on our server. Upon startup, it initiates a process for communicating with clie ...

Identify the moment all Dropzones are added to a form

I am working on a page where multiple dropzones are set up for individual images. Once the user submits the form, all the images attached to the dropzones are resized and then added to the rest of the form fields. After resizing and appending the images, ...

SetBootstrapTooltip at the location of the mouse pointer

Is it feasible to implement a bootstrap tooltip that follows the mouse cursor in AngularJS? If not, what other options are available for achieving this functionality? ...

Discover classes dynamically and eliminate redundant div elements

I am facing an issue with a JSON request that dynamically generates search results within div elements. The search results contain duplicate classes, all starting with "lid". How can I programmatically identify and remove all but one of the duplicate class ...

Can you explain the functionality of the container prop within the Drawer component of material-ui?

While delving into the official documentation for material-ui's Drawer component, I stumbled upon a mysterious container prop that is not explicitly mentioned in the API documentation. Could this possibly be one of the inherent props of a React compon ...

Divide the identical elements and distinct elements from a provided array into two separate arrays storing unique elements and recurring elements

Can anyone help me with this query? I have an array of objects that need to be separated into repeating and non-repeating objects based on the segments they belong to, each in a separate array. For example- [ {name:"abc",id:1,segments:[1,2]}, {n ...

Saving solely the content of an HTML list element in a JavaScript array, excluding the image source

One issue I am facing is with an unordered list in HTML which contains items like <ul id="sortable"> <li class="ui-state-default"><img src="images/john.jpg">John</li> <li class="ui-state-default"><img src="images/lisa.jpg ...

Creating a stroke in SVG using JavaScript

I created a code that inserts a line into my svg page when I press a button Here is the html snippet: <body> <svg width="500" height="400"> </svg> <button id="btn1">Append text</button> </body> Below is the script us ...

"Explore the versatility of React Day Picker with customizable months and weekdays_long

I have implemented the following packages: "react": "^18.2.0", "react-day-picker": "^8.1.0", and I am attempting to translate the months and days into French. However, despite passing the translated arrays to my < ...

converting time stamps to written text

Is there a JavaScript library available that can convert a timestamp (Date Java class value) to text? For example: Just two minutes ago 4 hours back 23 August 2009 Does such a library exist? If not, I'll take on the challenge of implementing it ...

Is it possible that when a user is redirected to a different URL, Chrome terminates any pending Ajax requests?

Currently, I have a function that allows the user to unlock a list they are currently editing: function cancelLock(id) { $.ajax({ type: "POST", url: "/ajax.php?action=cancelLock", dataType: 'json', data: "id="+ id }); retur ...

Getting started with Pyspark: Grouping data and generating a key value pair column

I have some data that looks like this: Col1,col2,col3 a,1,# b,2,$ c,3,% I want to add a new column with col2 as the key and col3 as the value, like this: Col1,col2,col3,col4 a,1,#,{1:#} b,2,$,{2:$} c,3,%,{4:%} Is there a way to do this using pyspark? ...

What is the best approach to modifying array elements using onChange and hooks in React?

I'm struggling to implement something simple, and the solutions I've found so far don't quite address my specific issue. Here's the state in question: const [formFields, setFormFields ] = useState({ formTitle: '', fiel ...

Having trouble uploading Node.js and Mongoose to Heroku due to error codes H12 and H15? Need help troubleshooting and resolving this issue?

Attempting to deploy my Node, mongoose, express app on Heroku for the first time has been a challenge. The application is a simple blog with a login system. Despite extensive research and effort, I am struggling to successfully host it. Below is the error ...

Seeking assistance in optimizing my Javascript code for more efficient canvas rendering

I've created a script for generating random moving lines as a background element for my portfolio. While it runs smoothly on its own, I encounter frame drops when combining it with other CSS animations and effects, especially at the beginning (althoug ...

What is the best way to store multiple forms in a single request using React?

Is there a more efficient way for me to save multiple forms in multiple child components from the parent component using just one API request? I have attempted to utilize Context and reducer, which did work. However, I am unsure if this is the best approa ...

What is the best way to send a JSON response from a Symfony2 controller?

I am utilizing jQuery to modify my form, which is created using Symfony. The form is displayed within a jQuery dialog and then submitted. Data is successfully stored in the database. However, I am unsure if I need to send some JSON back to jQuery. I am ...