What is the method to retrieve the complete name of the primary key field (country name) in Angular JS using HTML

Utilizing AngularJS and Lodash, I am making a call to an API that returns JSON data structured as follows:

[{ "_id":"zw",
   "name":"Zimbabwe"   
},
{ "_id":"au",
   "name":"Australia"   
},
{ "_id":"in",
   "name":"India"   
}]

To map country codes to their respective full country names, I have created the following function:

$scope.CountryHashmap  =  _.reduce(res,  function (hash,  value)  {
          var  key  =  value['_id'];
          hash[key]  =  value['name'];
          return  hash;
        },  {});

Upon executing the _.reduce function, the mapped data looks like this:

{
"zw":"Zimbabwe",
"au":"Australia",
"in":"india"

}

On my HTML page, I already have country codes present, and I aim to replace these codes with the corresponding full country names.

HTML:

{{countryCode}}

The countryCodes may include au, in, and zw.

Answer №1

Retrieve the country name from $scope.CountryHashmap by accessing the countryCode key.

{{CountryHashmap[countryCode]}} 

You could alternatively define a function for this purpose.

$scope.getName(code){
    if(code)
        return $scope.CountryHashmap[code];
}

Your request is not very clear. :)

Answer №2

To properly access country data, simply use the country code key to retrieve information from the mapped data array like mappedDataArr[key].

var app = angular.module("myApp", []);

app.controller("MyCtrl", function($scope) {
    $scope.mappedDataArr = {
      "zw":"Zimbabwe",
      "au":"Australia",
      "in":"india"
    };
});
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

</head>
<body>
<div ng-app="myApp" ng-controller="MyCtrl">
    <span>{{ mappedDataArr['in'] }}</span>
</div>
</body>
</html>

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

Modify the color scheme of an HTML webpage

Looking to enhance my HTML page with a new feature. The page is responsive and currently using Bootstrap CSS and JS. My goal is to allow users to change the color of the page dynamically by selecting a background or text color. Check out this example link ...

The output of the incorrect .bind() example is not as

I recently came across the .bind() method while learning JavaScript. The example I found on MDN here was quite helpful, but I decided to add some console.log() statements for better understanding. this.x = 9; // Here 'this' refers to the glob ...

The onload function for a JSP embedded in an IFrame was causing the IFrame to expand and fill the entire browser

I am encountering an issue with an IFrame inside a DIV where the SRC attribute is being dynamically set by a JavaScript function. When the SRC is set to "file.jsp", an onload function within the file.jsp containing style adjustments was causing the IFrame ...

Customize numbers in JavaScript with a Unity-inspired design changer

I am currently working on implementing a number input feature that allows users to adjust values by clicking and holding the mouse button while moving the cursor left and right, similar to Unity's editor number adjuster: https://youtu.be/uY9PAcNMu8s?t ...

Encountering an "Unspecified Reference Error" while attempting to retrieve data from an API in your

I've been attempting to utilize a mock API from in order to fetch data for my Next.js application. However, I am consistently encountering an undefined reference error, despite following the code snippet provided in the official Next.js documentation ...

Convert the PHP variable into a JSON object

i am currently working on a project with Codeigniter below is my PHP switch-case section case 'check': $balance = $this->Model_transactions->getUserBalance($this->input->post('userId')); $needToPay = floatval($this->inp ...

Webpack 2.7.0 throws an error: "Unexpected parameter: theme"

At the moment, I am on webpack 1.16.0 and using --theme as an argument to set the output path and plugin paths. The command appears as: rimraf dist && webpack --bail --progress --profile --theme=<name of theme> However, as I try to upgrade ...

Issue encountered when attempting to showcase the events in the calendar

Hello everyone, I am currently working on an event module where I have encountered an issue. After creating multiple events on the same day, only the first event is displayed in the popup when clicking on the date in the calendar. The Template Engine bei ...

What is the reason for jQuery's ajax feature running scripts by itself?

Lately, I've observed an issue where jQuery functions seem to disappear if a jQuery ajax call is made immediately after injecting jQuery into an inner iframe. The functions like `.dialog()`, `.draggable()`, and other plugins stop working when the ajax ...

Get an ICS file as text using JQuery

As a newcomer to JQuery and JS, I seek your understanding for any mistakes I may make. My current goal is to extract the text from an ICS file (e.g. BEGIN:CALENDAR....) using JavaScript. Here is a simple HTML file I am working with: <html> <b ...

The functionality of Angularjs ui-modal is being affected by issues related to the $http request

Currently, I am incorporating Angularjs 1.5.x along with AngularUI Bootstrap. My goal is to initiate a bootstrap modal and populate it with remote data by making use of $http request. To achieve this, I am utilizing the resolve method within the modal&ap ...

Adding an element within an ngFor iteration loop

I'm currently working on a code snippet that displays items in a list format: <ul> <li *ngFor="#item of items">{{item}}</li> </ul> These items are fetched from an API through an HTTP call. Here's the code snippet for tha ...

The Parse-JS-SDK currently doesn't support using the objectId in the matchesKeyInQuery method

javascript "parse-server": "^2.6.3", "parse": "^1.10.0", In my database, there are three tables: Member, Circle, and MemberCircle. The Circle table has a pointer field called member, which indicates who created the circle. On the other hand, the Memb ...

Using NodeJS to retrieve the mean price from the OPSkins pricelist

Is there a way to calculate the average price of every skin listed in this document? I'm wondering how one would go about using the dates and prices in order to determine the 60-day average price. My approach would involve extracting the data from t ...

VBA Hover Clickthrough Technique

I have successfully logged in, but I am having difficulty clicking on the hover image/text. While I can use the base URL and the href provided in the source code to navigate, I would need to re-enter login information. Additionally, the page that the URL n ...

Implementing the DRY (Don't Repeat Yourself) principle across a group of related

I'm struggling with applying the DRY principle. Can someone assist me in creating a single function that can achieve the same results as these 4 functions? I also need guidance on how to call this function for each .symbol. function changeGradient(in ...

I need PHP code to automatically add row numbers to the beginning of each row when fetching data from a MySQL database

Here is my PHP code snippet: $query = Select * from tablename; $result = mysql_query($query) or die((mysql_error())); $count = 0; if (mysql_num_rows($result) > 0){ while( $row = mysql_fetch_array($result)){ $json_output[$count]=$row; ...

I am currently working on obtaining images that are saved by their URL within a PHP file. These images are located within a directory named "images."

My code is incomplete and not functioning as expected. $.get("museums.php",function(data,status){ var response=''; //console.log(data); var json = $.parseJSON(data); museums = json.museums; for(let m in museums) { $("#na ...

What is the process for transferring information from HTML to Python and then receiving the output in HTML?

I am completely unfamiliar with the connection between HTML and Python, so I am reaching out for some assistance. I hope that someone here can lend me a hand. Currently, my HTML is hosted on an Apache server, and I access the website using the address "". ...

"Encountering issues with Instagram embeds.js library failing to load

I've been working on a JavaScript function that loads the HTML embed code for Instagram posts. When the post appears, the photo is replaced by a grey background and the Instagram logo. Even though the post contains other information like hashtags, tim ...