tips for organizing an array of strings

Looking to arrange an array object in Typescript based on a particular string value. I attempted the code below but it's not giving the desired result.

this.deviceapp = data.Devices[0].Products.sort((product)=>product.Status="Non-Compliant")

I aim to have "Non-Compliant" entries at the top, any suggestions on how to achieve this?

Answer №1

When you set product.Status="Non-Compliant", you are simply assigning the value "Non-Compliant" to product.status.

The .sort function requires -1, 0, or 1 as a return value. You can try the following approach:

let products = [
   { Status : "Compliant"},
   { Status : "Non-Compliant"},
   { Status : "Compliant"},
   { Status : "Compliant"},
   { Status : "Non-Compliant"},
   { Status : "Compliant"},
]

products.sort( product => product.Status==="Non-Compliant" ? -1 : 1)

console.log(products)

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

Using ngIf for binding

Trying to bind values based on conditions specified in *ngIf. However, when using the && operator within *ngIf, it seems to be behaving mysteriously. Sample Code: <div *ngIf="days.sunday == true"> <p class="circle ml-3" ...

Issue with pre-selected default value in AngularJS TypeScript Kendo UI DropDownList

I have successfully implemented a list of objects for items, but now I am facing a challenge in adding a default selected value. Below is the HTML code for the drop-down: <select kendo-drop-down-list k-options="selectItems" k-ng-mode ...

Retrieve the height of a div element in an AngularJS framework, and assign this value to a corresponding attribute of another element

Is it possible to retrieve the height of an element using AngularJS and assign it to another div's attribute? In my Bootstrap website, I have a fixed sidebar that needs to stop before reaching the footer. To achieve this, I currently have to manually ...

Adding a button label value to a FormGroup in Angular

I've been working on a contact form that includes inputs and a dropdown selection. To handle the dropdown, I decided to use the ng-Bootstrap library which involves a button, dropdown menu, and dropdown items. However, I'm facing difficulties inte ...

"Exploring the world of Semantic UI event triggers

Exploring the semantic UI framework has been a recent endeavor of mine, but I'm struggling to grasp how events are triggered within it. Take, for example, changing the "onchange" event of a drop down list: $('.ui.dropdown').dropdown(' ...

Incorrectly parsing data in ReactJS leads to errors

After making significant progress, the focus of this question has strayed too far. Therefore, it will not be continued. *===== The issue at hand revolves around fetching data from an API, receiving results, and encountering difficulties when attempting t ...

Tips on extracting specific information from a JSON object within an array using a key from a different object in Hugo

I am currently working with a data file in Hugo that contains information structured like the following: { "schedule": { "week1": [ { "day": 0, "away": 1, "home": 2, ...

Adjust the position of an SVG element based on its size using a transformation

How can I offset an SVG element relative to its own size using translate with a percentage, similar to how we would offset a div using transform: translate(100%,0)? Here is an example: This code: <div style={{ overflow: 'visible', position: ...

Redirecting after executing JavaScript code

Is it possible to implement a redirect after the subscription script for push notifications has been executed successfully? <script> var OneSignal = window.OneSignal || []; OneSignal.push(function() { OneSignal.init({ ...

Creating number inputs in Ionic 2/3 using alerts

I am currently working with Ionic 3.x on my macOS system. The issue I am facing is as follows: I have an array that contains a number and another array consisting of names. table: { number: number, names: string[] } = { number: 0, names: ['& ...

Ensure to verify the presence of a null value within the ngFor iteration

I have a webpage where I am displaying information in buttons. These buttons show various objects from a list along with their corresponding fields (e.g. object.name, object.age, etc.). Sometimes, one of those fields is null. How can I check if a value is ...

Adding persistent active classes to navigation elements within the navbar context using AngularJS

I am working on a navigation list element in AngularJS and I want to store the active tab so that it adds the 'active' class whenever there is a reroute. Currently, my goal is to simply log the name of the active tab. Since I have never linked a ...

When populating an Angular select dropdown from an array, the selected option appears as

I am having trouble displaying my strings array in a select dropdown. Initially, I used the standard method: <form [formGroup]="emailForm" #form="ngForm" (ngSubmit)="sendEmail()" novalidate class="tooltip-center-bottom ...

Issue with Material UI scrollable tabs failing to render properly in Internet Explorer

Currently, we are integrating Material UI into our tab control for our React Web UI. Everything is functioning smoothly in Chrome, but when we attempted to test it in IE, the page failed to load and presented the error below: Unhandled promise rejection ...

tips for utilizing a variable for inferring an object in typescript

In my current code, I have the following working implementation: type ParamType = { a: string, b: string } | { c: string } if ('a' in params) { doSomethingA(params) } else { doSomethingC(params) } The functions doSomethingA and doSomething ...

Does the onchange function in the dropdown list only work when the first item is changed?

Here is a snippet of my HTML code featuring a list item generated from a database using a foreach loop: <select class="form-control select" id="inventoryitem" name="inventoryitem" onchange="getunit();"> <option>---Select an item---</o ...

Creating buffer geometries for spheres using Three.js

Currently, I am working on a three.js project that involves displaying numerous spherical objects. To achieve this quickly, I decided to utilize buffergeometry. Upon researching, I came across a helpful post here, where I learned that I could transform nor ...

Removing duplicates from a multidimensional array by comparing the first element in each subarray using Javascript

I need to obtain unique values from a multidimensional array. The uniqueness should be based on the first element of each item. let arr = [['item 1', 'item 2'],['item 1', 'item 5'],['item 3', 'item 4& ...

Fullcalendar.js plugin experiencing issues with click event functionality

I'm currently working on implementing a calendar in VueJS and I have specific requirements: Need to display Month view Want to include Week view Clicking on a cell should show Day view Data needs to be displayed in each cell of the calendar Unable ...

What are the steps to deactivate react-number-format input?

Is there a way to deactivate input for the react-number-format component even though it doesn't have a built-in disabled property? ...