Accessing data from an API in an AngularJS dropdown menu

After retrieving JSON data from an API, I store it in $scope.industry.

$scope.industry = [];

    $http.get('/industrygroup?languageid=1')
        .then(function (result) {
            $scope.industry = result.data;
        });

The JSON data stored in $scope.industry holds information about industries.

To populate my dropdown menu, I use ng-option and specify the values to display as:

ng-options="p.Name for p in industry[0].Occupations"

Currently, the dropdown shows occupation names. However, I want it to display industry names instead. Here is a snippet of the JSON data structure:

{
      "Language":{
         "Id":1,
         "Name":"English"
      },
      "Occupations":[

      ],
      "Id":2,
      "Name":"Food and Beverage"
   } 

I aim to display the Name "Food and Beverage" in the dropdown menu. This JSON example represents one row returned by the API, and I wish to show all Names excluding the Language Name.

Answer №1

To retrieve all the names from result.data, you can utilize underscore in the following manner:

let uniqueNames = _.uniq(_.pluck(result.data, 'Name')));
$scope.namesList = uniqueNames;

An alternative approach could be:

p.Name for p in industry

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

Can JavaScript be utilized to retrieve the MAC address?

Is it possible to retrieve the Mac addresses of a system for login using JavaScript? ...

Using eventKey in React Router with Lodash

I'm attempting to use lodash to create a dynamic set of links that can easily be modified. {_.map(leftItems, item => ( <Menu.Item> <Link {...item} eventKey={{ ...item.eventkey }} />{' '} </Menu.Item> ...

Waiting for Promise Js to be fulfilled

I've been exploring the use of Bluebird for handling promises in Node.Js. I have encountered a situation where I need a function to return only when a promise is fulfilled. The desired behavior can be illustrated by the following code snippet: functi ...

Do specific elements of typography adjust according to the size of the window?

I'm always amazed by the creative solutions that come out of this community. So I'm reaching out to see if anyone knows a way to achieve something unique with CSS! Specifically, I'm curious if it's possible to make certain horizontal p ...

utilizing the data provided by a user in the "prompt" window within my HTML code

Looking to utilize user input from the prompt window. This is my custom JavaScript form: var answer; function load() { answer=prompt("what is your name?", "Write your name here"); return answer; } function hideNsee() { var HNS = docume ...

AngularJS: Error - Angular object is undefined

Hello! I am currently working on a project to develop a simple App using c# WebAPI and AngularJS. Unfortunately, I have encountered an error in the console which is preventing the web app from functioning properly. Here is a snippet of my Index.html file: ...

Issue encountered in React 18: The value on the left side of an assignment statement must be a variable or a property that can be accessed

After upgrading my React version from 17 to 18, I encountered an error with this code: data?.area?.width=['111','220']. The error message states "The left-hand side of an assignment expression must be a variable or a property access." W ...

Difficulty transferring form information to PHP utilizing Ajax

Originally, I had a traditional HTML form that submitted data through a button, redirecting the user to the page where the PHP code ran successfully. Now that everything is functioning as expected, I aim to pass the form variables to PHP using Ajax in ord ...

Unable to send JSON object using the Jquery.post() method

I have a specific object that is created within my javascript application. poll_data[active_question] = { 'question': $('div.question_wrap textarea').attr('value'), 'answers': [ $(&a ...

Working with JSON data in postgresql

I am currently working with a postgres database that houses student information such as their names, passwords, and the groups they belong to. This data is stored in JSON format. I am attempting to extract specific data points for each category, which are: ...

Instructions for resolving the Sys.ArgumentTypeException error: The object of type 'Object' cannot be converted to type 'Function'

I encountered a specific error in my JavaScript function that I'm struggling to resolve. Uncaught Sys.ArgumentTypeException: Sys.ArgumentTypeException: Object of type 'Object' cannot be converted to type 'Function'. Parameter name ...

Utilizing Express.js for reverse proxying a variety of web applications and their associated assets

I am looking to enable an authenticated client in Express to access other web applications running on the server but on different ports. For instance, I have express running on http://myDomain and another application running on port 9000. My goal is to re ...

Updating comment content using Ajax

I am in the process of using Ajax to insert my comment content. However, I seem to be facing some issues with the comment_add.php page. I was hoping someone could review it for me. While I have successfully obtained the streamid as checked in firebug, no ...

Efficiently finding a group of substrings within a JavaScript string

I am currently working on a method to efficiently search for specific substrings within a given string. Here is my current implementation: const apple = "apple" const banana = "banana" const chickoo = "chickoo" const dates = & ...

Ways to navigate a div within an iframe that has been loaded

As I load a page(A) inside an iframe, the HTML structure of the embedded content is as follows: <html><body> <div id="div1"></div> <div id="div2"><button>Hello</button></div> </body></html> The ...

Having trouble with ion-item click not functioning in Ionic 3?

Working on my Ionic 3 project, I have encountered an issue with the ion-item click listener. The scenario is that I am adding text over a canvas and then attempting to change the style of the text (such as font-family, font-size, bold, and italic). The UI ...

"Error message when iterating through JSON using jQuery: 'a' is

I've come across similar discussions on this topic, but I haven't found a solution for looping through an array of key-value pairs in the following format: { "quotes": [ { "author": "Author One", "quote": "Quote One" }, { ...

iOS posts being rejected by Devise Registrations controller

Currently, I am in the process of developing a RoR backend for an iOS application. My approach involves using Devise (3.2.4) to secure the API. Nevertheless, I encountered an issue while attempting to register a user via http://localhost:3000/users as prov ...

You need to pass a function as the first parameter when using passport-local-mongoose as a schema plugin

I've been struggling with implementing passport-local-mongoose in my server. I followed a tutorial video but encountered an issue that wasn't addressed in the video - even though my database is connected to mongodbatlas. The error message "First ...

How to Load JSON Data Using D3 When Column Definitions are Separated

I'm currently working with d3.js and struggling to grasp how I can effectively load a JSON file representing a table that has separate column definitions. A typical JSON file, which I have no issue loading, might appear like this: [ { "id": 1, ...