Using AngularJS to showcase the values of various object properties in an input field, separated by commas

Within my JSON object array, the structure is as follows:

users=[{id:'1',name:'ABC'},{id:'2',name:'DEF'},............]

To present the names of all users in a single input text field separated by commas, I am aiming for an outcome similar to the image below.

https://i.sstatic.net/Yhuak.jpg

I attempted utilizing the ng-list directive. However, this necessitated individually iterating through each user object, extracting and storing their names in a distinct array, which would subsequently serve as the ng-model for the <input> element. Are there any simpler or alternative approaches within angularjs?

Answer №1

An effective solution in JavaScript involves utilizing the Map function to extract the desired property from an array and then using a join method to combine the values.

 var users=[{id:'1',name:'ABC'},{id:'2',name:'DEF'}];

 $scope.userModel = users.map(function(el){return el.name}).join(",");

 <input type="text" ng-model="userModel">

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 utilizing "Koa-Rewrite," an AssertionError occurs stating that app.use(rewrite) necessitates a generator function

I am currently working with Koa@1 and koa-rewrite@1 to implement a specific functionality. rewritelogic.js const rewrite = require('koa-rewrite'); function rewriteLogic(){ rewrite('/english/experience/dev1', '/english/experienc ...

Chrome experiences a temporary halt when attempting to load a webpage

My website is hosted on an Apache2 server and I am encountering issues specifically with Google Chrome. There are instances when the main page loads smoothly, but at other times, Chrome seems to get "stuck" displaying a small box that says waiting for doma ...

Randomly choose one of 5 pages and insert an HTML element at different intervals using JavaScript, jQuery, MySQL, or PHP

Suppose we have the following HTML element: <img src="icon.png"> Our website consists of 5 pages: index.php products.php register.php about.php terms.php How can we randomly place this HTML element on one of the pages, ensuring it stays there for ...

Fresh to Angular: Understanding Calculated Variables

Transitioning from Knockout to Angular has presented me with a few challenges. It seems like I might be approaching things in a non-angular manner. http://jsfiddle.net/LostInDaJungle/BxELP/ I've provided the link to jsfiddle instead of including my ...

What are the steps to secure an API endpoint using PassportJS?

Within my app, I have integrated Express and AngularJS for functionality. Specifically, I am utilizing express to manage the basic web serving of the angular code through static routes. The angular code interacts with services that connect to API endpoin ...

Unable to use setState on a component that is not mounted despite already calling componentDidMount

I'm facing a peculiar React Router issue that has me puzzled. Despite the fact that I can confirm the component in question is executing its componentDidMount lifecycle method, I am constantly receiving the warning "Can't call setState (or force ...

Exploring the properties of a file directory

As I try to access the d attribute of a path that was generated using Javascript, the output of printing the path element appears as follows: path class=​"coastline" d="M641.2565741281438,207.45837080935186L640.7046722156485,207.0278378856494L640.698 ...

Autocomplete Data Origin

I'm currently exploring the use of Typeahead and implementing AJAX to fetch my data source: $(document).ready(function() { $('input.typeahead').typeahead( { hint: true, highlight: true, m ...

Guide on replacing characters in Blogger using Javascript

I'm trying to change certain characters like [ngu1], [ngu2] to [chanquadi] using the following script: <script type='text/javascript'> var heo1 = document.querySelector(<.post-body>); var heo2 = heo1.replace("[ngu1]", "c ...

Modify the information and return it to me

I am attempting to modify and return the content inside a custom directive (I have found some resources on SO but they only cover replacement). Here is an example: HTML <glossary categoryID="199">Hello and welcome to my site</glossary> JS . ...

Substitute any text string starting with a colon, such as the route path in Express

Below are some sample strings: const a = '/example/:someItemUuid/hello' const b = '/example/:someItemUuid/hello/:otherItemUuid' const params = { someItemUuid: '12345', otherItemUuid: '67890' } I am in search o ...

Issues with padding in Bootstrap grid system

I'm currently working on a website using the bootstrap grid system. Although I have not used the row class, I am facing an issue with the padding between the divs. <div class="container"> <div class="col-lg-4"></div> <d ...

Sending Various Parameters to PHP API Function

Currently, I am a PHP newbie and I am in the process of modifying an API that was built using the Laravel Framework. Within my controller, I have an API function that looks like this: public function DeleteOneMail(Request $request) { $uid = $request-& ...

I am trying to map through an array fetched from Firebase, but despite the array not being empty, nothing is displaying on the

I retrieved an array of products from a firebase database using the traditional method: export const getUsersProducts = async uid => { const UsersProducts = [] await db.collection('products').where("userID", "==", uid).get().then(sn ...

Determine the height of an element in JavaScript or jQuery by using the CSS property height: 0;

I'm facing a challenge in determining the actual height of an element, which currently has a CSS height property set to: height: 0; When I check the console, it shows a height of 0, but I'm looking to find the real height of the element. I als ...

Switching sub components based on routing through navigation links after logging in

I'm having an issue with my routing setup while transitioning from the login page to the main page. Here's how my Routes are structured: App.jsx <BrowserRouter> <Routes> <Route path="/main/" element={<Main ...

Developing a model in NodeJS poses a challenge for me

While attempting to create a model in NodeJS const mongoose = require ('mongoose'); const User = mongoose.model('User',{ name:{ type:string }, lastname:{ type:string }, age: { type:Number } }) module.exports = User; **errorIS ** C: ...

Accessing values using keys in a Groovy name-value array

My array consists of values as shown below: def fieldValuesArray = [] fieldValuesArray << ["key1":"value1"] fieldValuesArray << ["key2":"value2"] During iteration, I am looking to extract the key values ...

Ways to bring in external javascript files in reactjs

I'm currently working on a form that requires the user to input their location. To achieve this, I have integrated the npm package react-geosuggest-plus. However, I want to avoid including <script src="https://maps.googleapis.com/maps/api/js?key=AI ...

tracking scroll position within div on main page

I have a div tag enclosed within a content tag due to the implementation of a masterpage containing the forms and body tags. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> <div id="xxx" style="overflow:s ...