The arrangement of a JSON array can be modified by AngularJS' Ng-repeat functionality

Hello everyone at SO:

On March 18, 2014, I encountered a situation while trying to use ng-repeat. The elements inside the array, retrieved from a Json string, seem to be changing their original order.

To clarify, the initial variables in the array pertain to order_id and person name, whereas the later ones relate to credit information.

The information seems to be jumbled up, perhaps sorted alphabetically, although I cannot be certain.

There doesn't appear to be anything unusual in the code:

<li ng-repeat="(variable, value) in records" >
     <a href="">{{variable}}: </a>
     <a href="">{{value}}</a>
</li>

Any suggestions or references?

Thank you in advance! Chris;

Answer №1

chris: In the recent release of Angular 1.4, the issue with sorting keys alphabetically has been resolved. According to the official Angular documentation:

Version 1.4 no longer sorts keys alphabetically. The order is now based on the browser's return when using for key in myObj

https://docs.angularjs.org/api/ng/directive/ngRepeat

Answer №2

To achieve this, you can utilize the orderBy filter. Below is an illustration:

Assume you have a controller structured as follows:

function Controller($scope) {
  $scope.friends =
      [{name:'John', phone:'555-1212', age:19},
       {name:'Mary', phone:'555-9876', age:10},
       {name:'Mike', phone:'555-4321', age:21},
       {name:'Adam', phone:'555-5678', age:35},
       {name:'Julie', phone:'555-8765', age:29}]
  $scope.predicate = '-age';
}

In your HTML:

<div ng-controller="Controller">
    <table class="friend">
      <tr>
        <th>Name
        <th>Phone Number</th>
        <th>Age</th>
      </tr>
      <tr ng-repeat="friend in friends | orderBy:predicate">
        <td>{{friend.name}}</td>
        <td>{{friend.phone}}</td>
        <td>{{friend.age}}</td>
      </tr>
    </table>
  </div>

The output will be:

Name    Phone Number    Age
Adam    555-5678        35
Julie   555-8765        29
Mike    555-4321        21
John    555-1212        19
Mary    555-9876        10

This should address your concern effectively.

If you want to sort by age in ascending order, modify this line in your controller:

$scope.predicate = '+age';

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

When using Codeigniter with AJAX dropdown menus, some issues may arise if form validation fails and the view gets redirected or reloaded

After the validation process fails and redirects back to the same controller index, an issue arises where the second dependent dropdown stops functioning. if (!$this->form_validation->run()) { $this->index(); } The form consi ...

Could not load ngx-restangular due to an error: (SystemJS) Module not yet loaded while trying to load "@angular/core"

Recently, I made the switch from using the AngularJS 'restangular' library to the Angular 'ngx-restangular' library during an upgrade from AngularJS to Angular. However, after the transition, I encountered an unexpected error along wit ...

Angular routes do not populate the $state in resolving promises

Recently, I encountered a situation where $stateParams were populated in one instance but not in another. Being new to angular-ui-router, I could use some assistance on this matter. Any tips would be greatly appreciated. Thank you! When I added $statePara ...

Express app: the ideal location to implement a closed-loop temperature control system

I am relatively new to working with express.js, having only created some basic client/server apps in the past. Currently, I am looking to develop a temperature controller using a PID component. However, I am struggling to grasp the architecture of express ...

JavaScript method to prevent users from entering numbers as the first two characters, with all subsequent characters being numbers only

I need a specific requirement for my form. The textbox field in my form is labeled "DEA License number", and I want it to only allow the user to enter alphabetic characters for the first two characters, followed by numbers afterward. I am looking to impl ...

Enhancing ASP.NET MVC 5 Application with jQuery Validate: Implementing Submit Button Click Events

In the process of developing an ASP.NET MVC 5 application, I find myself faced with a dilemma. The current setup involves using the jQuery Validate plug-in that comes packaged with MVC applications. Upon clicking the "submit" button, jQuery Validate kicks ...

Is it more advantageous in Vue to pre-process and save data directly to the data property, or to utilize computed properties?

I am interested in hearing diverse perspectives on this topic. When working with Vue (and possibly other frameworks), is it more beneficial to prepare non-reactive data through a method and store it in the data for use in the template, or is it preferable ...

Navigating through Index in #each in emberjs

Take a look at the code provided below: http://jsbin.com/atuBaXE/2/ I am attempting to access the index using {{@index}}, but it doesn't seem to be compiling. I believe that handlebars should support this feature: {{#each item in model}} {{@index} ...

Updating characters in a char array

Struggling with json escaping in a complex framework while creating a JSON response. I have a char[] with the text that needs to be escaped. What is the most efficient way to perform this, considering it involves replacing one character with two characters ...

Retrieve information in JSON format from a document

I'm trying to extract data from a JSON file without knowing the exact location of the data. Here is an example JSON: var names= [ { "category":"category1" , "name1":"david", "name2":"jhon", "name3":"peter" }, { "category":"catego ...

Is it possible to use the HTML script tag without specifying the type attribute as JavaScript? <script type="text/html"></script>?

While examining the source code of an HTML page, I stumbled upon the following snippet: <script id="searchItemTemplate" type="text/html"> <# var rows = Math.floor((Model.RecordsPerPage - 1) / 3 + 1); for (var i = 0; i < rows; ++i){ ...

Building a DataFrame from JSON data using danfo-js, mongoDB, and mongoose on a Windows 10 platform: A guide

I'm currently facing a challenge in constructing a DataFrame using danfo-js with JSON data retrieved from mongoDB / mongoose. Just to provide some context, the dependencies I am using are danfojs-node: ^0.1.5, mongodb: ^3.3.4, and mongoose: ^5.7.12. ...

Update the data table after a new file has been uploaded

As a novice web developer, I embarked on my first project using Vue. In this project, I created a form for file uploads in Vue 2 and Laravel. If you want to take a look at the full code: View: https://pastebin.com/QFrBfF74 Data table user file: https:/ ...

Transferring values from jQuery AJAX to Node.js

Is there a way to successfully pass a variable from jQuery to nodejs without getting the [object Object] response? I want to ensure that nodejs can return a string variable instead. $('.test').click(function(){ var tsId = "Hello World"; ...

Adjusting Headers Using Buttons

Having some issues with my JavaScript code. I'm trying to achieve a specific functionality where clicking the "Modify HTML content" button changes the h1 heading from "The Original Content" to "The New Content", and vice versa on subsequent clicks. Si ...

I need help converting the "this week" button to a dropdown menu. Can someone assist me in troubleshooting what I am missing?

Seeking assistance with customizing the "this week" button on the free admin dashboard template provided by Bootstrap 4. Looking to turn it into a dropdown feature but unable to achieve success after two days of research and watching tutorials. See code sn ...

The pairing of Transpiller and Internet Explorer 8 is like a dynamic

In starting my new project, I am considering using BabelJS. However, there is a significant requirement that must be met: it needs to be compatible with IE8. ISSUE: Babel compiles ES6 to ES5, but the support for ES5 on IE8 is lacking. Are there any alter ...

Tips for nesting maps in React without causing redundant renders

I have a list of all the items in the array let products = [ { name: "iPhone 12", storage: "64GB" }, { name: "iPhone 12 Pro", storage: "256GB" }, { name: "iPhone 12 Pro Max", storage: "512GB" ...

Ways to retrieve directory information in Next.js hosted on Netlify

Having trouble retrieving a list of directories in Next.js on Netlify. The code works fine on localhost, but once deployed to Netlify, an error is triggered: { "errorType": "Runtime.UnhandledPromiseRejection", "errorMessage": ...

Inquiring about the integration of CodeIgniter with Javascript and AJAX

Recently delving into Codeigniter and strategizing for a substantial application. Feeling a bit perplexed about CI's handling of JS files and AJAX requests. Opting for mod_rewrite with my project. In the typical webpage setup, I'd link indiv ...