Arranging the items in a specific sequence - Angular

I am dealing with an object of objects and I need to display these objects and their values in a specific order (not alphabetically).

How can I accomplish this using angular? Below is a sample of my object:

var filters = {
    language : { someProperty : "prop1", someOther : "prop2" },
    country : { resumeProp : "prop", resumeProp2 : false },
    destination { resumeProp : "prop", resumeProp2 : false },
};

I would like to organize the objects as destination, country, and then language.

Answer №1

In the world of JavaScript, objects are inherently unordered (as stated in the ECMAScript Language Specification, section 4.3.3). It's not guaranteed that iterating over a set of object properties twice will yield the same order both times.

To ensure order, one should opt for an array and leverage the Array.prototype.sort method:

var filters = [
    { name: "language", order: 2, someProperty : "prop1", someOther : "prop2" },
    { name: "country", order: 1, resumeProp : "prop", resumeProp2 : false },
    { name: "destination", order: 0, resumeProp : "prop", resumeProp2 : false }
];

function compare(a,b) {
  if (a.order < b.order)
    return -1;
  else if (a.order > b.order)
    return 1;
  else 
    return 0;
}

filters.sort(compare); // destination, country, language

If you're working with ES6, consider using the Map object; it operates like an Object but guarantees key order as well.

If altering the original object is not feasible,

  1. generate another array featuring the object's indexes, such as

    var filtersKey = ['destination', 'country', 'language'];

  2. apply the ng-repeat to this new array.

  3. fetch values from the original object via filters[value], where value corresponds to each string within filtersKey accessed through the ng-repeat.

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

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 ...

Having trouble with your Mocha tests in IntelliJ? Breakpoints not working as expected during debugging?

I'm currently facing an issue with debugging my Mocha test in IntelliJ 13.1.4. I have inserted breakpoints in my test/test.js file to help with the process. Upon checking my Node.js Run/Debug Configurations, I have ensured that the Node interpreter i ...

Calculating the function using data in a Vue component

Here is a Vue component along with some data: Vue.component('receipt', { template: '#receipt-template', data: function() { return { tip: 8.50 }; }, computed: { subtotal: function( ...

Jquery Visualization Chart not displaying

I am struggling to get the jquery visualization to work properly. Although the table and caption appear fine, there is no data showing up in the chart. I've carefully followed the example and searched for any issues, but I can't identify what&apo ...

Angular 2 decorators grant access to private class members

Take a look at this piece of code: export class Character { constructor(private id: number, private name: string) {} } @Component({ selector: 'my-app', template: '<h1>{{title}}</h1><h2>{{character.name}} detai ...

Switching views in AngularJS by using a controller to control the content displayed in the

My JavaScript web app utilizes AngularJS to simplify tasks, but I've encountered an issue. I'm trying to change views from an ng-controller using $location.path, but for some reason, the view isn't updating even though the path in the $loca ...

Utilize a Chrome Content Script to intercept jQuery delegated event handlers and take control

After developing a Chrome extension that intercepts form submissions in specific circumstances, I encountered an issue with a particular website utilizing jQuery's delegate function. My extension is built with raw JavaScript, excluding jQuery to prev ...

Issue with Attaching Click Event to Dynamic Div Elements

I've implemented divs with a Click Event triggered by a value entered in a text box. See an Example Here Upon opening the page, clicking any rows will trigger an alert. However, if you change the value in the text box (Enter Number) and click load, ...

Understanding the default value type in React's setState method

I am new to React and facing difficulties identifying errors in my code. My goal is to display a list of users retrieved from a server using socket.io. Below is the original code snippet: // list of users const [users, setUsers] = useState() useEffect(() ...

How can I modify the card loading style in Vuetify?

My preference is for the <v-card :loading="loading">... However, I would like to modify the appearance from a linear progress bar to something like an overlay. I am aware that changing colors can be done by binding color instead of using boolean ...

Trouble presenting information retrieved from API

I'm encountering an issue with displaying the data I fetched from an API. I'm not sure what's causing the problem... I attempted to use the map() function to access the data, but it's not functioning as expected either. import React fr ...

What could be causing the unusual alignment of the 'pixels' on my resized HTML5 canvas?

Currently, I am in the process of creating a simple HTML5 canvas/JavaScript snake game inspired by the classic Nokia Snake. This is my first attempt at developing a game using HTML5 canvas and JavaScript, and I must admit, I am still a novice programmer! ...

Display an "add to cart" button and a discount image when hovering over

Currently, I am in the process of developing an Online Shopping website using PHP. To enhance the design of my website, I have implemented bootstrap. One specific query I have is how to display an 'add to cart' button and a discount image when a ...

Height of inline-block list items in ul is not properly adjusted

I am working on a horizontal navigation bar using inline-block for the li tags. Here is the code snippet: <ul> <li><a href="#">HOME</a></li> <li><a href="#">FEATURES</a></li> <li><a href ...

What is the best way to implement a delay in axios requests within a loop array?

I am currently working on a project in Vue where I need to add a delay to axios requests within a loop involving an array. let promises = []; for (const item of this.itemsWithIndex) { const cmd = "od_kioskPaperUpdate"; ...

How to prevent blur from overwriting the value of an event with its previous value

Utilizing a common function to update the values of two inputs, here is an example: <b-input v-model="ownerProps.approver2ExtraCost" @blur="onClick($event)" class="inputBuefy" ></b-input> </div> ...

The jstree does not seem to be generating the tree structure as expected based on

I am utilizing the jstree plugin to construct a hierarchical tree view of locations, rooms, and assets for a company within a PHP application that I am developing. The main intention behind this tree is to enable users to select an asset while going throu ...

"Learn how to extract the image URL from the configuration file (config.json) within the assets folder, and then seamlessly display it within

In my Angular project, I have a configuration file located in the assets folder: { "brandConfig": "brand1", "brand1": {"urlPath": "http://192.168.168.60:8081/mantle-services", " ...

Determine whether the click occurs inside or outside of a bar on a ChartJS graph

I'm currently working with a bar graph using chartJS. I'm trying to figure out how to detect where the user clicked - whether it was inside or outside of the bar region in chartJS. const waterFChart = new Chart(canvasRef.current, { plugins: [ ...

Why is it necessary in JavaScript to reset the function's prototype after resetting the function prototype constructor as well?

Code is often written in the following manner: function G() {}; var item = {...} G.prototype = item; G.prototype.constructor = G // What is the purpose of this line? Why do we need to include G.prototype = item before resetting the prototype? What exact ...