Utilize AngularJS to enhance user experience by replacing the service response value with a more user

Looking to enhance the user experience by displaying a more friendly value in a table that utilizes ng-repeat. Consider the response model from the service below:

[{
   Name : "Hon", 
   Gender : "M"
},
{
   Name : "Hon", 
  Gender : "F"
},
{
   Name : "Hon", 
 Gender : "M"
},
{
   Name : "Jon", 
  Gender : "M"
},
{
   Name : "on", 
  Gender : "F"
}]

The current table layout is as follows:

Name Gender

Hon M

Hon F

Hon M

Jon M

on F

However, the desired table format would be:

Name Gender

Hon Male

Hon Female

Hon Male

Jon Male

on Female

Avoiding the use of any for loop for this task is preferred. While this example is simplistic, my scenario involves multiple columns requiring similar mapping and potentially hundreds of records per page.

Is it possible to achieve this using directives or filters in AngularJS? If so, how can this be implemented? Or do you have alternative suggestions?

Answer №1

Angular's $filter also utilizes a looping logic.

If you decide to implement an angular filter, the following code snippet could guide you:

.filter('genderTransform', function() {
  return function(input) {

    return input.map((val) => {
      if (val.Gender == 'F') {
        val.Gender = 'Female';
      } else if (val.Gender == 'M') {
        val.Gender = 'Male';
      }
      return val
    })
  }
})

Afterward, in your template:

<div ng-repeat="item in data | genderTransform">
   <!-- customize your content here -->
</div>

Feel free to check out this functional demo

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

Receiving undefined when subscribing data to an observable in Angular

Currently, I am facing an issue in my Angular project where subscribing the data to an observable is returning undefined. I have a service method in place that retrieves data from an HTTP request. public fetchData(): Observable<Data[]> { const url = ...

Choose a variety of photos for each individual input file

I am facing a scenario where I need to allow users to upload 3 images using input[type="file"]. Each input should only accept 1 image, but if a user selects more than 1 image in the first input, I want to distribute them among the subsequent inputs. For in ...

The AngularJS function invoked itself within the structure

When working with my form, I encountered a problem involving custom input directives and text fields. In addition to these elements, there are buttons: one for adding a new input to the form which is linked to the function $scope.AddNewItem() in the contro ...

Even with the use of !event.repeat, the keyboard event still manages to trigger twice

I've been working on an interactive online piano that takes keyboard input, but I'm encountering a problem where sometimes it registers two events instead of one when a key is pressed. To address this issue, I attempted to use e.repeat (with e r ...

The initial update of ng-model is not occurring

I am currently working on a checkbox functionality in my project that is bound to an ng-model: <input type="checkbox" ng-change="toggleAll(globalChecked)" ng-model="globalChecked"> The toggleAll function is responsible for accessing the globalCheck ...

Troubleshooting the pre-loader image loading problem

Apologies for bringing up a minor issue, but I'm struggling with this one. I added a pre-loader image to my page load using a simple method. You can find my page here Here is the HTML code for the Pre-loader image: <div class="se-pre-con">&l ...

Retrieve characters preceding and following a space (JavaScript)

In my Angular component, I have a phone number field. For example, +36 42534534534 I am trying to extract the code before the space and the phone number after the space. This is how I am currently handling it: set phoneNumberResult(value: string) { ...

Unable to retrieve HTTP call response during debugging, although it is visible in the browser

When I send an HTTP request to create a record, I am able to see the added record id in the Network section of browsers like Chrome and Firefox. However, when I try to debug the code and retrieve the same id value, I encounter difficulties. I have tried us ...

PHP redirection cannot be directly translated to JavaScript redirection

I'm having an issue with my PHP file redirecting to an HTML file. The structure of my website is as follows: HTML ==> separate JS file (using Angular) ===> PHP file When the PHP file calls the header function, instead of redirecting, it displ ...

Using JavaScript Regular Expressions to locate all prefixes leading up to a specific character

Consider a scenario where you have a string consisting of terms separated by slashes ('/'), like this: ab/c/def Your goal is to identify all the prefixes of this string up to a slash or the end of the string. For the given example, the expected ...

Unable to determine the reason behind the code getting stuck in an endless loop

Check out this code snippet: import React from 'react'; import Card from './components/Card'; import './App.css'; import Buttongrp from './components/Buttongrp'; import { useEffect, useState } from 'react'; ...

The test in Gulp has not passed successfully

When attempting to run a gulp test, the following errors occur: Error: spyOn was unable to locate an object to spy on for logout() at /workspace/WebServices/src/test/javascript/spec/components/auth/auth.services.spec.js:9 TypeError: 'unde ...

Is it possible to create a "text carousel" using HTML, CSS, and JavaScript?

Currently, I am in the process of building a 'text carousel' on my own, without seeking assistance. The progress so far can be viewed here (please stretch it out, as it is not responsive yet). Specifically, I am aiming to replicate the text carou ...

Is it possible to use Three.js as the backdrop for a website design?

For a new project on my website, I am considering using three.js for an interactive experiment. My idea is to take an existing experiment that I already have the code for and integrate it as a dynamic background for my site. Does anyone have experience wi ...

When clicking initially, the default input value in an Angular 2 form does not get set

I am currently learning angular2 as a beginner programmer. My goal is to create a form where, upon clicking on an employee, an editable form will appear with the employee's current data. However, I have encountered an issue where clicking on a user f ...

The Access-Control-Allow-Headers does not permit the use of the Authentication request header field

I've noticed this question frequently popping up on Stack Overflow without a clear explanation of its significance. Could someone kindly elaborate on the meaning of this error? Request header field Authentication is not allowed by Access-Control-Allo ...

Encountering a problem while attempting to display an id dynamically using Vue.js?

AppTabs.vue <template> <div :class="{ 'flex space-x-4': variant === 'horizontal', }" > <ul class=" list-none bg-opacity-30 p-1.5 rounded-lg text-cente ...

Running a script through the terminal using an electron js application, but the process necessitates the installation of node js

I am currently working on a project that involves running a script through the system terminal using my Electron.js application. However, I am facing an issue with new users who may not be technically proficient and do not have Node.js installed on their s ...

Customize Input Values by Selecting an Option with jQuery-UI Autocomplete

Hello there, I am a newcomer to java-script and could really use some help. My goal is to have the data in the country field automatically populated when a user enters data into the city field. I have an xml file: <ROWSET> <ROW> <city>&l ...

Detecting the scroll events of elements with the overflow:hidden property

Looking to synchronize scrolling between two different panels or divs? In one element, there's an overflow: auto while the other has overflow: hidden (trying to mimic a grid with frozen columns). I've managed to sync the scroll when it occurs w ...