Ways to organize JSON information in JavaScript

The JSON data I have is structured like this.

 {"count": 3, "records": [
 {"common":{"review_date": "2016-02-07 07:00:00","severityid": {"value": "2", "description": "Medium"}}},
 {"common":{"review_date": "2016-02-07 08:00:00","severityid": {"value": "2", "description": "Serious"}}},
 {"common":{"review_date": "2016-02-07 08:00:00","severityid": {"value": "2", "description": "Minor"}}}
 ]}

I'd like to display it in the order of Serious, Medium, and then Minor.

{"count": 3, "records": [
 {"common":{"review_date": "2016-02-07 08:00:00","severityid": {"value": "2", "description": "Serious"}}},
 {"common":{"review_date": "2016-02-07 07:00:00","severityid": {"value": "2", "description": "Medium"}}},
 {"common":{"review_date": "2016-02-07 08:00:00","severityid": {"value": "2", "description": "Minor"}}}
 ]}

Answer №1

If you need to arrange the data in a specific order, consider using a helper object like this:

{
    High: 1, 
    Medium: 2, 
    Low: 3
}

var data = { "count": 3, "records": [{ "common": { "review_date": "2016-02-07 07:00:00", "severityid": { "value": "2", "description": "Medium" } } }, { "common": { "review_date": "2016-02-07 08:00:00", "severityid": { "value": "2", "description": "High" } } }, { "common": { "review_date": "2016-02-07 08:00:00", "severityid": { "value": "2", "description": "Low" } } }] };

data.records.sort(function (a, b) {
    var order = { High: 1, Medium: 2, Low: 3 };
    return order[a.common.severityid.description] - order[b.common.severityid.description];
});
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');

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 extracting data from a JSON-formatted API using Selenium WebDriver

I am currently making a GET call to fire a series of APIs that are returning data in JSON format. I am specifically looking to extract and print the value of the "hostId" from the JSON snippet provided below. "hostId" : 286, "parentSectionName" : "a ...

Issue with resetting the state of a react-select component remains unresolved

I'm currently facing two issues with my react-select component: Firstly, once I select an option, I am unable to change it afterwards. Second, when my form is reset, the react-select component does not reset along with the other fields. For simplici ...

Using Vue to handle Promise resolution - incorporating Laravel Gate logic into Vue

Trying to incorporate Laravel's authorization and policy into Vue has been a challenge for me. I'm working on creating a mixin that sends a GET request to a backend controller. The issue I've encountered is that the v-if directive is receiv ...

What is the reason for pandas sorting columns alphabetically when reading JSON files?

import pandas as pd data = pd.read_json("https://bitbay.net/API/Public/BTCPLN/trades.json?sort=desc") data What could be causing read_json to alter the columns order and what steps can I take to rectify it? ...

What is the process for installing fontawesome using npm?

I encountered an error while attempting to install that looks like the following: $ npm install --save @fortawesome/fontawesome-free npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\Admin\Desktop\package.json&a ...

My local server threw an unexpected error while running my React app: error code 0308010C, signaling an issue with the digital envelope

Error: error:0308010C:digital envelope routines::unsupported at new Hash (node:internal/crypto/hash:71:19) at Object.createHash (node:crypto:133:10) at module.exports (/Users/parasnagpal0707/Documents/Projects/StalkHub/node_modules/webpack/lib/ ...

Why does JavaScript not wait for the completion of forEach and instead executes the next line immediately?

While creating my api in nodejs and attempting to push the mongoose return count to a newly created array, it does not wait for the forEach loop to finish before executing json.res() and returning a null response. However, when I use setTimeout(), the re ...

What are the reasons why Bootstrap icons are not functioning properly in ReactJS?

I am facing an issue with bootstrap icons not working in ReactJS. Click here to view my buttons This is the code for my post items: import React from 'react'; const PostListItem = () => { return ( <li className="app-list ...

Unveiling the secret to accessing every UserName and UUID through Minecraft API

I'm currently working on a project that aims to replicate the functionality of NameMC, but I'm faced with the challenge of obtaining a comprehensive list of all Minecraft users. NameMC serves as an index for Minecraft users, showcasing their nam ...

How come the action doesn't trigger in my React/Redux application?

Currently, I am in the process of developing a small application that incorporates modals. Instead of relying on pre-made packages like react-modal, I have taken the initiative to build the modal functionality from scratch. 1) Defining a reducer located i ...

Share a Node.js Express.js npm package for universal access within the project

Here is my current folder structure. /app.js /src /routes /controllers Within the routes folder, there are multiple javascript files that all require the passport.js package like so: const passport = require('passport'); Is it possible to c ...

What is the process for retrieving randomized data using mongoose?

I recently came across the mongoose-random package which allows for retrieving a JSON array of random records using mongoose. My goal is to retrieve three random records with a specific field. Despite reviewing the documentation, I have yet to find a work ...

"Transmitting an ajax POST call and receiving a corresponding GET response

Currently, I am utilizing the following AJAX code: $.ajax({ url: "Editor.aspx/Page_LoadComplete", data: { "contentCheckCode": contents }, type: "POST", success: function (response) { alert("Contents saved..."); }, error: fu ...

Exploring error management in Vue3 and Vite when deploying to production

After following the error handler setup in the Vue documentation, I encountered a difference between using it on the development server and in production. The error handler effectively pinpointed the component and line number where the error occurred durin ...

Show or hide specific elements based on parameters using ng-show in Angular

One challenge I am facing is how to manage a menu with multiple buttons that open submenus without using a massive switch statement in my code. Instead, I want to try something different: In the HTML file, I call the function toggleVis and pass the nam ...

Error thrown: SyntaxError - Forbidden break statement in AJAX code execution

Trying to exit a loop nested within a statement has been a challenge. Despite researching similar questions on stackoverflow, I have not found a solution that works. Below is the current code in question: for (var i = 0; (i < 10); i++) { ...

Ways to switch visibility based on user's selection

Currently, I am working on a project that involves a select form with multiple options. However, I need to display a specific div only when a particular option is selected. Can anyone suggest the best solution for this scenario? Your guidance would be grea ...

angucomplete-alto automatically fills in data based on another input

Having two autocomplete select boxes with a unique feature has been quite interesting. The first input accepts a code that is related to a label, autofilling the second input with the corresponding object once the code is selected in the first input. Howev ...

Setting environment variables using the node command is successful on Linux and macOS platforms, however, it may not function properly

When I clone a project using git, I encounter issues running npm run build on Windows. The command works fine on Mac and Linux: "build": "API=https://dev-api.myexample.com/v1.0 babel-node build.js", An error message is displayed: 'API' is no ...

Retrieve data entries within a specified range of numbers

Retrieve all records in JSON format from 2 tables "posts_main" and "posts_comments" that are associated with a specific User. $sql = "select posts_main.*, (select groupid from posts_comments where groupid = posts_main.id group by groupid ) as count_commen ...