Order a portion of a JSON array according to another part of the same array

Having a json array that needs sorting in JavaScript. The EventName field should match the respective Age fields like 01-10 Days and 10-20 Days.

[
 {Age: "01-10 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []}
,{Age: "01-10 Days", EventName: "Invoice Distribution", Value: 5, ActiveInvoices: []}
,{Age: "01-10 Days", EventName: "Invoice Pay Pending", Value: 2, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Approval", Value: 2, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Distribution", Value: 11, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Paid", Value: 1, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Pay Pending", Value: 2, ActiveInvoices: []}
]

If you can assist with sorting this data properly, here is a jsfiddle link for reference: http://jsfiddle.net/h829p07n/1/

// Desired Output
[
{Age: "01-10 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []}
,{Age: "01-10 Days", EventName: "Invoice Distribution", Value: 5, ActiveInvoices: []}
,{Age: "01-10 Days", EventName: "Invoice Pay Pending", Value: 2, ActiveInvoices: []}             
,{Age: "10-20 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Distribution", Value: 11, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Pay Pending", Value: 2, ActiveInvoices: []}
,{Age: "10-20 Days", EventName: "Invoice Approval", Value: 2, ActiveInvoices: []}  
,{Age: "10-20 Days", EventName: "Invoice Paid", Value: 1, ActiveInvoices: []}
]

Thank you in advance for your help!

Answer №1

There are errors in the code, specifically in the last for loop where the variable count is not being utilized correctly. Check out a demo here.

for(var i = 0 ; i < tempArra.length-1 ; i++){
   count = tempArra[i].length;
    for(var k = 0; k < count ; k++){
        for(var L = 0; L < count; L++){
            if(tempArra[i][k].EventName != tempArra[i+1][L].EventName){
                    NewArray.push(tempArra[i+1][L]);
            }else{
                if(tempArra[i][k] != undefined){
                 CommonArray.push(tempArra[i][k]) ;
                }
                if(tempArra[i+1][L] != undefined){
                    CommonArray.push(tempArra[i+1][L]);
                }
            }
        }
    }
}

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

Populate HTML dropdown menu with data from an external JSON file

I'm attempting to populate a HTML Dropdown menu with information from an external JSON file, which consists of the following { "Destinations": [ { "destinationName": "London", "destinationID": "lon" }, { "dest ...

Filtering on the byte[] array key in a StructRowKey column in HBase

HBase Filtering Rows by Partial Row Key In my table, the key is a byte[] containing various components such as an identifier (a or p), an ID of variable length, a date with time in seconds, and additional IDs. However, I am interested in filtering based o ...

Tips for accessing the URL in PhoneGap and parsing the response with jQuery

Currently, I am working on a task in PhoneGap which involves creating a registration page. When the user clicks on the registration page, the values entered will be sent to a specific URL. If the user is already registered or not, the data will be returned ...

The new and improved Vue 3 computed feature in the Composition API

The temporary object appears as: tmp : { k1: { k2 : { k3 : [abc, def] } } To access k3 in the setup, it should be: tmp.value.k1.k2.k3[0 or 1]. I am looking to change its name to something like - k3_arr = tmp.value.k1.k2.k3; Within my Vue single componen ...

Getting a URL to redirect after a successful login via an AJAX request in PHP

I've been trying to figure out how to redirect the URL after a successful login using an Ajax call in PHP. Can someone please review my code and point out any mistakes? Here is the content of the registration.php file located at http://localhost:8080 ...

Retrieving the value of an item within a string array using VB.NET

Here is the code snippet I am working with: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim JsonSub As String = "{""subdomain01"":[""21"",""subdomain01"",""4""],""subdomain02"":[""22"","" ...

Error encountered while trying to serialize the JSON array

As someone who is relatively new to working with API's, I've encountered an issue where I'm getting a "graphconnection is not serializable" error when attempting to json.dumps an api response. Despite the response appearing to be in json arr ...

Accessing dataset schemas to parse JSON files

A JSON file has been included in a dataset within Foundry: [ { "name": "Tim", "born": "2000 01 01", "location": {"country": "UK", "city": "London"}, ...

Exploring Three.js: The challenge of loading multiple material objects

I am currently using three.js and facing an issue while loading multiple material objects. The problem arises when I rotate the code, causing unexpected behavior with the second material object. Ideally, I want the first material object [cubeMaterial] to r ...

AngularJS promises are known for returning the object itself instead of just the resolved value

function getBusTimetable() { var waiting = $q.defer(); var busData = $http.get('https://bus.data.je/latest'); busData.success(function(response) { waiting.resolve(response); }); busData.error(function(error) { waiting.reject( ...

Tapping on the invisible picture

I currently have a square image of a car with a transparent background. My goal is to make the car clickable so that when I click on it, it triggers an action. However, I also want the transparency around the car to allow clicks to go through and affect th ...

Preserve the visibility of a text input form while the checkbox is selected

Within my HTML code, there is a form that includes a checkbox labeled "other." When this checkbox is selected, a textbox will appear. If the user types in text and submits the form, the textbox disappears, but the checkbox remains checked (as saved in loca ...

Undefined will be returned if the data in AngularJS is not set within the $scope

I have developed a factory that contains a list of functions which are called when the state changes. On page load, I am calling dtoResource.rc1Step1DTO(). Although the function executes, when I check the result in the console, it returns undefined. Below ...

Tips on effectively managing an XMLhttp request to a PHP controller

Encountering an internal server error and nothing is being logged in the PHP error log. This issue persists despite not using any frameworks. DEV - controllers |-> user -> check_email.php - public_html |-> routes ...

Utilize Haxe Macros to swap out the term "function" with "async function."

When I convert haxe to JavaScript, I need to make its methods asynchronous. Here is the original Haxe code: @:expose class Main implements IAsync { static function main() { trace("test"); } static function testAwait() { ...

The $().bind function will not function properly unless the document is fully loaded

Do I need to include $(document).ready() with $().bind? Here is the HTML portion: <head> <script type="text/javascript" src="jquery-1.10.2.min.js"></script> <script type="text/javascript" src=&quo ...

What is causing the net::ERR_CONNECTION_RESET in Node JS and ExpressJS?

Our application, built on ExpressJS and NodeJS, is hosted on a Linode server and served at port 3000. Although the app has been functioning well for a year, we have recently encountered consistent connection errors. The errors vary, but they are mostly re ...

Different methods to disable scrolling when the mobile menu pops up

I have implemented a mobile menu option that appears when you click on the triple line logo, but unfortunately, users can still scroll while the menu is open. I've tried using position:fixed; but haven't been successful in preventing scrolling be ...

Creating a binary tree in vanilla JavaScript and styling it with HTML and CSS

I'm facing a challenge with my homework. I am required to convert my JavaScript binary tree into HTML and CSS, strictly using vanilla JavaScript without any HTML code. I have the tree structure and a recursive function that adds all the tree elements ...

Fetching data from a ColdFusion component using AJAX

Having trouble performing an SQL COUNT on my database using AJAX through a cfc file, and I can't figure out how to retrieve the return variable. Here's the relevant section of the cfc file: <cffunction name="getSpeakerCount" access="remote" r ...