Arrange Nested Object Array

I've been trying to sort a nested array, but I'm having trouble with it. It works when I specify the index, but that's not ideal...I need to loop through all values.

Array

items= [{
    app_type_id: 1,
    apps: [{

          app_id: 3,
          app_type_id: 1,
          app_name: "Test1"

        },
        {
          app_id: 2,
          app_type_id: 1,
          app_name: "Test2"

        },
        {
          app_id: 107,
          app_type_id: 1,
          app_name: "Test3"
        }
      }]
  }]

TS

this.iarray[0].apps.sort((a, b) => a.app_name[0] > b.app_name[1] ? 1 : -1)

The current code only sorts the first two items, but in reality, there could be more (dynamic) apps. How can I modify it to sort through all apps?

Answer №1

It seems like the error lies in your comparison of app_name values rather than the first and second characters.

A corrected version of your code might look something like this:

items.apps.sort((a, b) => a.app_name > b.app_name ? 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

Explanation of Default Export in TypeScript

I recently started learning about JS, TS, and node.js. While exploring https://github.com/santiq/bulletproof-nodejs, I came across a section of code that is a bit confusing to me. I'm hoping someone can help explain a part of the code. In this project ...

What is the process for implementing three conditions in a MongoDB query?

db.mycollection.find({"$and": [{"key1": value1}, {"key2": value2}]}) This particular query is designed for 2 filtered conditions. If I now want to introduce a third condition with key3 and value3, how should I modify the que ...

Find the closest point within a 2D numpy array

Despite reading numerous posts on finding the closest value in a numpy array or the closest coordinate in a 2D array, none of them address my specific issue. The challenge I'm facing involves having a 2D numpy array structured like this: [[77.6288173 ...

How can I fix the TypeScript error stating that "Dayjs is missing properties from type Date" that occurs while using Day.js alongside Material-UI DatePicker?

As I integrate Day.js with Material-UI's DatePicker component in my React project, I encounter an issue. Setting the minDate and maxDate properties to Day.js objects results in a TypeScript error: Type 'Dayjs' is missing the following proper ...

Analyzing an array that contains a single string versus an array with multiple plain texts to determine if the single string is included in

After receiving no response to my previous question, I decided to simplify it. I have one array with ( "superman", "batman", "flash") and another array with ("superman is the strongest superhero", "batman is the most badass of all") Now, superman an ...

Having trouble retrieving information from a nested JSON array within a JSON object using JavaScript

I am facing a challenge with extracting specific information from a JSON object that contains an array nested inside. Even though I have experience working with JSON arrays, this particular object is proving to be tricky. For example, when attempting to r ...

Learn how to utilize the combineLatest/zip operators to only respond to emissions from the second observable while disregarding emissions from the first observable

Here's an example of how I'm initializing a property: this.currentMapObject$ = zip(this.mapObjects$, this.currentMapObjectsIndex$, (mapObjects, index) => mapObjects[index]); I want the value of this.currentMapObject$ to be emitted only ...

Is it possible to globally modify the component reference <dropdown-component> name in Angular during runtime in a dynamic manner?

I am currently working on an application that utilizes a component called "dropdown-component" throughout its pages. However, due to specific business requirements, I have been tasked with replacing "dropdown-component" with "custom-dropdown-component". Un ...

The GraphQl Code Generator fails to correctly generate the graphql() function in Next.js applications

While working on my next.js project, I integrated GraphQL to generate types for queries. However, the code generator is not functioning properly and displaying an error message: "The query argument is unknown! Please regenerate the types." within the gql.t ...

Unable to connect input with abstract classes at a hierarchy depth of 2 levels or more

When working on my Angular application: If a Component utilizes an Input that is defined in its immediate parent (abstract) class, everything runs smoothly. However, if a Component uses an Input that is declared in a parent class located two levels a ...

Encountering an issue while adding types to a custom component in a Formik form: Error message states that type 'T[keyof T]' cannot be assigned to type 'string'

I'm delving into TypeScript for the first time and attempting to incorporate it into a previous project as a learning exercise. I've hit a roadblock while trying to add types to a custom form field component in Formik, encountering an error mess ...

Progress bar for file download is not visible on the browser

I have developed an Angular application that utilizes an ASP.NET Web API. My goal is to enable users to download files stored on my server. The current code I am using for this functionality is as follows: [HttpGet] [Route("downloadFile")] [JwtAuthentica ...

Executing a TypeScript file in Sublime Text

Is there a way to compile a TypeScript file (.ts) in the sublime text 3 console and display the output similarly to how sublime handles Python? Since TypeScript is a compiled language, it needs to convert the .ts file to a .js file before execution. How ca ...

How to Share Angular Modules Between Two Projects with Ivy Compilation Necessity

Query: I am faced with the challenge of sharing common modules between two Angular projects, one of which requires full Ivy compilation to function properly. To manage these shared resources, we have set up a private GitHub NPM repository. However, becaus ...

Developing an Angular component using data retrieved from a JSON response

I want to design a model/class for my Angular App using the provided response template: { "id": {integer}, "name": {string}, "make": { "id": {integer}, "name": {string}, "niceName": {string} }, "model": { "id": {string}, "n ...

Iterating through lines within a String Array

In my program memory, I have an array containing text const char* const paragraph[] PROGMEM = { "First Line", "Second Line" }; What is the method to read each string individually and output them one at a time? First Line Second Line ...

What is the best method to compare two times and determine if they fall on the same date within an Angular 2/4 application? The time should be in the format of "HH:mm AM

Is there a way to validate if my time period falls on the same date? let startTime = currentSelection.startTimeHr + ":" + currentSelection.startTimeMin + " " + currentSelection.startTimeAMPM; let endTime = currentSelection.stopTimeHr + ":" + currentSele ...

Exploring directory organization in GraphQL Queries using GatsbyJS

In my portfolio, I have organized my work into categories, pieces, and pictures in a cascading order similar to a child-parent relationship. The folder structure reflects this hierarchy, with the main problem being explained in more detail below. Folder s ...

Unable to modify the active property of the specified object as it is read-only

Presented here is the interface: export interface ProductCommand extends ProductDetailsCommand { } This is the ProductDetailsCommand interface: export interface ProductDetailsCommand { id: string; active: boolean; archive: boolean; title: ...

Looking for a JavaScript (Angular) event listener to trigger when closing pages and tabs

I am looking for an event that will only work when closing a page or tab, but should not be triggered when the page is refreshed. I am aware of the "beforeunload" event, but it also gets activated on page refresh. Below is the code snippet I am currently ...