Arranging a nested JSON array directly

Below is the structure of my JSON data :

Root
|- cells []
     |-Individual cells with the following
                         |- Facts (Object)
                         |- Measures (Object)
                                 |- Key value pairs
|- other values

My goal is to sort the values in the measures object in-place. Is this possible? If not, what would be the most efficient approach? One option could be to extract the values into a temporary array, sort them, and then rearrange the keys in the JSON structure. However, this doesn't seem like an optimized solution.

I am attempting to achieve sorting in the jsHypercube library : https://github.com/thesmart/js-hypercube . I have searched the library but couldn't find a built-in sort method.


Here is a sample JSON snapshot :

The desired output is a sorted array of _cells based on any key in the measures object, such as Cost, Profit, or Revenue.


Based on the feedback from @FelixKling, I believe the data should be sorted prior to serialization into a JavaScript Object. The initial data before serialization :

Answer №1

Thanks to the helpful suggestion from @FelixKling, I successfully implemented a code snippet to sort the data with ease. Sharing it here in case it proves useful to others.

tempCube.sort(function(a, b) {
        var valueA, valueB;        

        valueA = a.data.Price; 
        valueB = b.data.Price;
        if (valueA < valueB) {
            return -1;
        }
        else if (valueA > valueB) {
            return 1;
        }
        return 0;
    });

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

Is memory allocated for 32 bits for variables with an undefined value in Javascript?

If I have a function with the signature below: function test(variable1, variable2) {} And I call it with only one parameter: test(5); Then within the test function, variable2 will be created but with a value of undefined. I'm interested to know if ...

AngularJS Modals within Modals

I have a website where clicking a button triggers the opening of a modal window. Below is the essential controller code for this functionality: var modalOptions = { animation: true, templateUrl: 'somehtml.html', controller: ' ...

Building a TypeScript Rest API with efficient routing, controllers, and classes for seamless management

I have been working on transitioning a Node project to TypeScript using Express and CoreModel. In my original setup, the structure looked like this: to manage users accountRouter <- accountController <- User (Class) <- CoreModel (parent Class o ...

Having trouble resolving "react-native-screens" from "node_modules eact-navigation-stacklibmoduleviewsStackViewStackViewCard.js"? Here's how to fix it

Here is the command I used for setting up react app routes: npm i react-native-router-flux --save After restarting npm with "npm start," I encountered this error message: Unable to resolve "react-native-screens" from "node_modules\react-navigation- ...

Difficulty maintaining list formatting in AngularJS and Bootstrap due to ng-repeat functionality

I'm currently working on a project where I need to display content from an array using ng-repeat in Angular. The content is originally in JSON format, but it has been stringified before being added to the array. The problem I am facing is that this c ...

Refine an Array by applying multiple filters

Here is a simple JSON array that I have: const personList = [ { id: 1, name: "Phil" }, { id: 2, name: "Bren" }, { id: 3, name: "Francis Underwood" }, { id: 4, name: "Claire Underwood" }, { id: 5, name: "Ricky Underw ...

Tips for rendering nested objects and arrays within Angular 2

I am receiving JSON data from an API on the back-end and I want to display it using ngFor. However, when I tried to do so, I encountered an error message stating: "Cannot find a differ supporting object '[object Object]'" in the console. To addr ...

The Google Software Development Kit has encountered a 403 error, indicating that the user has exceeded their

Currently, I am working on integrating the Google Developer SDK into my project to leverage the Google Translate functionality. While everything works smoothly when making a request using my access token obtained from the Google Developer Console, I keep e ...

Leverage variable as an expression when utilizing ng-include

Here is an example of working code: <div ng-include src="'Test.html'"></div> However, this code does not work: <div ng-include src="ctrl.URL"></div> (When ctrl.URL is set to "Test.html"). I have also tried setting it t ...

PHP scheduler alternative

I have a PHP script called updater.php that performs a task for 1-2 minutes. It is crucial for the script to complete its job. While I can schedule it with cron, I have come up with an alternative solution. The script should only run at specific times wh ...

Issue with setting GeoJSON in jQuery callback using Mapbox`

Using Jquery, I am trying to retrieve geojson points data (which will set markers on a map) from a database. function remm(id){ $.ajax({ type: "POST", url:"geojson2.php", data:{ u_id:id[0], t_id:id[2] }, success: functi ...

Initiating an AJAX request to communicate with a Node.js server

Recently, I started exploring NodeJS and decided to utilize the npm spotcrime package for retrieving crime data based on user input location through an ajax call triggered by a button click. The npm documentation provides the following code snippet for usi ...

Securing a route using a referrer in Node.js: Best practices

Within my node.js application, I am looking to secure a specific route so that users can only access the page /post if they are coming from /blog. If the user accesses the route from any other source, they should be redirected to /. I have implemented the ...

What could be causing the EBUSY: resource busy or locked, open errno: -4082 error to appear while trying to build storybook 7 in next.js 13?

While attempting to create a storybook, I encountered the following error in my project: [Error: EBUSY: resource busy or locked, open 'C:\Users\ali\Desktop\Works\Design Systems\projects\storybook-test2\node_modu ...

Adding JSON Objects in Python

In Python 3.8, I have successfully loaded two JSON files and now need to merge them based on a specific condition. Obj1 = [{'account': '223', 'colr': '#555555', 'hash': True}, {'account': ...

Here is a method to determine the indices of the floating point numbers within an array once it has been sorted using numpy's

I attempted to use np.partition to sort a list of float numbers, but it seems to be sorting them incorrectly. I suspect the issue lies with the presence of float numbers in the list. How can I properly sort an array list containing float numbers using np.p ...

The JOI validation process is failing to return all error messages, even though the "abort early" option

I have been encountering an issue while trying to validate my payload using a joi schema. Instead of returning the errors specified in the schema, only one error is being displayed. Even when I provide a payload with name as "int", it only shows one custom ...

In Angular, when a component is clicked, it is selecting entire arrays instead of just a single item at a

I am currently working on implementing a rating feature using Angular. This component will be used to rate different languages based on how proficient I am with them. My issue lies in the fact that when I click on one array element, it automatically selec ...

What is the best way to access HTML attributes within a JavaScript function when working with a dynamic element?

When a user interacts with an HTML element on my website, I aim to display the attributes of that specific element in a new browser tab. An example of such an HTML element is: <a id="d0e110" onclick="GetWordFile(this.id)" attr1="attr1_value" attr2="at ...

Tips for Converting Map to JSON Format in Scala

In my programming project, I have a Map with a single key and value pair called myDictionary. After populating this map, my goal is to write it in JSON format to a text file for future parsing. Initially, I used the following code: Some(new PrintWriter(o ...