Sorting objects with Javascript

users[usernames] = {
    userName        : username,
    userId          : id,
    userStatuINT    : statu,
    userMobilemi    : mobile,
};

Console log :

 console log(JSON stringify(data));

Output :

{
  "Guest-77":
      {"userName":"Jack","userId":"l1YeHSMYWvqUNgPPpvxE","userStatuINT":9,"userMobilemi":false},
  "Guest-47":
      {"userName":"Carter","userId":"zsq3Qcpd9qGw3X6kpvxF","userStatuINT":0,"userMobilemi":false},
  "Guest-68":{
      {"userName":"Alex","userId":"jmstDvTTLhZCLRW7pvxG","userStatuINT":4,"userMobilemi":false} 
} 

While loop :

$.each(data, function(key, value){
   // sorting...
});

Hello. Could you please advise on how I can sort the data based on the "userStatuINT" field?

Many thanks.

Answer №1

To organize properties in an object, consider transferring the data to an array and arranging it there.

For instance:

var newArray = [];
Object.entries(data).forEach(([key, value]) => {
  newArray.push({ id: key, info: value });
});
newArray.sort(function(a,b){
  var numA = a.info.userStatus;
  var numB = b.info.userStatus;
  return numA == numB ? 0 : numA < numB ? -1 : 1;
});

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

Steps to access the Link URL when the class name transforms to display as block:

I'm currently working on a function that needs to trigger a click on a link only if a specific classname is set to display: block; Unfortunately, my coding skills are not advanced enough to accomplish this task. Here is what I have so far: https://j ...

Error: Unable to assign a value to the length property of [object Object] due to it having only a getter method during the conversion process

Here is the code snippet that I am currently using with Node version 4.2.5 and [email protected] xls-to-json. function convertXLStoJSON(inputfile, outputfile, sheetName) { node_xj = require("C:/Protractor_Scripts/node_modules/xls-to-json"); no ...

Comparison of various approaches for invoking JavaScript/jQuery functions

Do the following examples have a performance variation? Example 1: $(window).on('resize', abc); function abc(){ //some abc code } Example 2: $(window).on('resize', function(){ //some abc code }); If so, what are the positives ...

The React Router's Switch component fails to update when the route is changed

In my component, I have a list of news categories. The links to these categories are in another component within the Router. However, when I change the link, the content does not update. I suspect this is because my NewsFeed component, where I define the S ...

Exploring Node.js: Comparing Intl.NumberFormat and decimal.js library for floating point calculation

Can someone explain the distinction between Intl.NumberFormat and the decimal.js library? If Intl.NumberFormat can handle currency calculations, what is the purpose of using the decimal.js library and what advantages does it offer over Intl.NumberFormat ...

Can a TypeScript file be created by combining a declaration file and a .js file?

It is commonly understood that declaration files are typically used for libraries rather than projects. However, let's consider a scenario where an existing JavaScript project needs to be migrated to TypeScript by creating d.ts files for each source ...

Repeated attempts to initiate ajax script failing to function

I am completely new to the world of Ajax, having just started learning about it a few days ago. Despite my lack of experience, I need to incorporate it into a form that I am creating for my employer. Unfortunately, I have been facing difficulties in getti ...

Unable to resolve the issue with ExpressPeerServer not being recognized as a function in server.js

I'm facing an issue with the peer.js library in my npm project. I have successfully installed it, but when I try to use it in my server.js file, I get an error saying that peerServer is not a function. const express = require('express'); con ...

The android webview is having trouble loading HTML that includes javascript

I have been attempting to showcase a webpage containing HTML and JavaScript in an android webview using the code below. Unfortunately, it doesn't seem to be functioning properly. Can someone provide assistance? Here is the code snippet: public class ...

How to effectively manage the default API quota in YouTube Data API v3 while ensuring requests are made every 60 seconds

Recently, I've encountered a challenge concerning the management of the default API quota for YouTube Data API V3, which allows 10,000 daily requests. In my JavaScript application, I need to fetch the number of subscribers and concurrent viewers every ...

What's the reason for not being able to customize classes for a disabled element in Material-UI?

Currently, I am utilizing Material-UI to style my components. However, I am facing challenges when trying to customize the label class for disabled buttons. Despite setting a reference as "&$disabled", it does not yield the desired results. import Rea ...

Exploring Illumination with Three.js

I'm interested in exploring the light properties further. I am curious about the variables used in the DirectionalLight.js and SpotLight.js source codes. Could you explain the difference between castShadow and onlyShadow? Is there a way to manage th ...

How can we wrap the Vuex store within a Vue plugin's install function?

I developed a plugin that utilizes Vuex for state management. // plugin.js import Vuex from "vuex"; import store from "./store.js"; export default { install(Vue, options) { const storeInstance = new Vuex.Store(store); Vue.pr ...

Guide on validating an Australian phone number with the HTML pattern

When it comes to PHP, I have found it quite simple to validate Australian phone numbers from input using PHP Regex. Here is the regex pattern I am currently using: /^\({0,1}((0|\+61)(2|4|3|7|8)){0,1}\){0,1}(\ |-){0,1}[0-9]{2}(\ | ...

Is it possible to update the useContext value within a child component so that all other child components can access the updated value?

Is there a way to set a default value (like null) in a parent context and then change that value in a child component so other children can access the updated value? For example, imagine we have an App.jsx file where a userContext is created and passed do ...

Each time the server restarts, Express.js will run a function asynchronously

Looking for guidance on implementing a function in my Express.js app that can fetch data from the database and then cache it into Redis. The goal is to have this function executed only upon restarting the Web server. Any suggestions on how I can achieve t ...

Connecting different domains using AJAX

Is it possible to send an Ajax request to a separate server instance (running on a different port) on the same machine? ...

Test fails in Jest - component creation test result is undefined

I am currently working on writing a Jest test to verify the creation of a component in Angular. However, when I execute the test, it returns undefined with the following message: OrderDetailsDeliveryTabComponent › should create expect(received).toBeTru ...

Despite the correct value being displayed in the console.log, the Textfield is not responding to the Reducer

I am currently working on a project to create a tool that can iterate through the pupils of a school class. In order to achieve this, I have implemented a text field in a react component that displays a value: <input className="form-control" onChange={ ...

Transforming Facebook data from JSON into structured objects using the JSON.NET library

One issue I am facing is that when I receive a list of posts from Facebook (page feed), there are times when multiple comments are stored in an array within the post object. Surprisingly, only the first comment of the post gets parsed correctly while the s ...