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

Is it possible for me to use AJAX to load content from a string? I am attempting to postpone the activation of certain HTML

I need help optimizing my HTML page that includes certain sections with large Javascript files and images, which are initially hidden. Even when set to "display: none," the browser still loads all the content. One solution could be moving those sections in ...

Is having async as false really detrimental?

Splitting my inquiry into two sections. Within my website, I am dynamically generating some divs by utilizing ajax post requests to retrieve data from the database. Following is the structure of my setup. <html> <body> <script type=" ...

Retrieve distinct keys from a JSON file in Node.js

Below is an example of JSON data: [ { "Name": "User1", "primaryRegion": "US" }, { "Name": "user2", "primaryRegion": "US" }, { "Name": &q ...

Cloudflare SSL Error 522 Express: Troubleshooting Tips for Res

After setting up my express project using express-generator, I decided to make it work with a cloudflare SSL Certificate for secure browsing over https. My express app is running on port 443. Despite my efforts, when I try to access the domain, I encount ...

AngularJS - Oops! Looks like we encountered an error: [$injector:modulerr]

I have a client-server application and I am facing an issue with this error message: "Uncaught Error: [$injector:modulerr]". Despite searching for solutions, none of them seem to be fixing the problem. Here is my client-side code: angular.module('m ...

Determining the Testing Status of a Node Package Management (NPM) Package

As someone who is new to Node.js and NPM, I have a question that may seem naive. Is there a method to determine if a package published on NPM has been tested? And if so, can this process be automated? Are there any tools or frameworks available that can va ...

Common mistakes made while working with decorators in Visual Studio Code

Having trouble compiling TypeScript to JavaScript when using decorators. A persistent error message I encounter is: app.ts:11:7 - error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the ' ...

What is the proper way to execute a JavaScript function within a JavaScript file from an HTML file?

I have the following content in an HTML file: <!DOCTYPE html> <!-- --> <html> <head> <script src="java_script.js"></script> <link rel="stylesheet" type="text/css" href="carousel.css"> & ...

How to import a dynamic typescript file in Next JS

As per the NextJs documentation, dynamic import can be used for JavaScript files like const DynamicComponent = dynamic(() => import('../components/hello')). Is it also advisable to use dynamic imports for .tsx files in a similar manner? ...

Floating action button within a collapsible panel

I am having trouble placing a fixed-action-btn inside a collapsible body. It keeps appearing in the bottom right corner of the page instead of within the collapsible itself. How can I ensure that the button stays inside the body? Check out this link for r ...

Ways to update the component's state externally

I'm new to Next.js (and React) and I'm attempting to update the state of a component from outside the component. Essentially, I am conditionally rendering HTML in the component and have a button inside the component that triggers a function to se ...

Whenever I try to import a directory that contains modules, Webpack encounters an error

I am currently in the process of developing a small npm library to streamline API interaction. Here is an overview of my folder structure... dist/ index.js src/ index.js endpoints/ endpoint1.js package.json webpack.config.js Inside my src/index ...

What is the process for configuring VSCode for C/C++ development on a Mac using Clang or GCC compilers?

Struggling to get Visual Studios Code set up on my Mac, particularly with the launch.json and task.json files. I'll be using the Clang compiler. While trying to follow Microsoft's documentation, I found that the .JSON files are configured to com ...

Decode a JavaScript string without the need to save it as a file

Imagine a situation where I allow a client to input a JavaScript script that needs to be sent to my server. This script is meant to export an object. Here is the frontend code snippet: <form action="/blabla.js" method="post"> ...

Retrieve the external JSON file using AngularJS

Attempting to retrieve JSON data using Angular JS has been successful with static data. However, encountering issues when trying to fetch data from an external JSON file. Below is the code and corresponding output: index.html <!doctype html> <ht ...

Load the React component asynchronously while waiting for the data to be fetched

My ReactJS component looks like this: import React, {useState} from 'react'; import Plot from 'react-plotly.js'; import {utility} from "./utility"; function Chart() { const [use_data, setData] = useState([]); c ...

What is the best way to deactivate div elements once an overlay has been applied to them?

My goal is to place an overlay on my form to prevent users from accessing the content. Even though I have added an overlay, users can still interact with input fields. How can I prevent that? .overlay { background: rgba(0, 0, 0, .75); text-align: ce ...

Exploring the possibilities of integrating the iTunes Store customer reviews API

Recently, I started working with the iTunes store customer reviews API and it has raised some queries for me. The standard URL format looks like this: https://itunes.apple.com/us/rss/customerreviews/id=(APPID)/sortBy=mostRecent/json One question on my mi ...

What is the proper way to reference a property's value within another property of the same JavaScript Object?

Within a Gulp.js file (or any Javascript file), I have defined paths in a Javascript object: var paths = { devDir : 'builds/development/', devDirGlobs : this.devDir+'*.html' } I am attempting to reference the pro ...

Is it possible to utilize rspec for conducting feature testing on an integrated rails-angular application?

My Rails-Angular application is very basic and integrated. It simply renders a link, fetching data from a GET request to Rails: 'use strict'; angular.module('angularRspec.controllers') .controller('FightersController', ...