Ways to cycle through an array of objects and display a specific property

I am working on a Sharepoint display template that involves manipulating an array of objects to achieve a specific output.

Name1
Name2
Name3

My goal is to customize the rendering of multiple people user field in Sharepoint by implementing a tooltip feature.

However, I am facing a challenge with iterating through the array and concatenating the values:

See screenshot below:

Code snippet:

// Customizing Sharepoint List View - Show Tooltip for Long String 
// By: [Your Name]

(function () { 

    var projectTeamContext = {}; 
    projectTeamContext.Templates = {}; 
    projectTeamContext.Templates.Fields = { 
        "Project_x0020_Team": { "View": ProjectTeamTemplate } 
    }; 

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(projectTeamContext); 

})(); 

function ProjectTeamTemplate(ctx) { 

    var projectTeamValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name]; 

    //newBodyvalue will contain all display names and be automatically rendered as a tooltip

    return "<span title='" + projectTeamValue + "'>" + newBodyValue + "</span>"; 

} 

Answer №1

To transform property values from the projectTeamValue array objects into a new array, you can use the map method followed by joining those values together with ", " as the separator:

var newBodyValue = projectTeamValue.map(function(person) {
    return person.value;
}).join(", ");

If your projectTeamValue array was structured like this:

[{ value: "Name1" }, { value: "Name2" }, { value: "Name3" }]

Then the resulting newBodyValue would be:

"Name1, Name2, Name3"

Note: The Array.prototype.map() function may not be supported in IE 8 and earlier versions but should work fine in other browsers.

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

It appears that the fetch operation does not wait as expected

Having some trouble with a caching system in my code where I'm trying to load the same template quickly. Even though I've tried implementing a basic caching mechanism to only fetch the template if it hasn't been loaded before, I'm strug ...

Searching with Sequelize for an indirect relationship: How can it be done?

Within my database, there exists a relationship between Users and Roles. A User can have multiple Roles assigned to them. These Roles are connected to Activities in such a way that they can be associated with many different activities. This association all ...

Where should the npm install command be executed?

I'm trying to incorporate a frosted glass effect into my project, but I'm unsure of where to place the npm install frosted-glass --save command. I use Atom as my code editor and already have Node.js installed, but I can't seem to figure out ...

Retrieve information from HTML form

I am facing an issue with a form that includes an input field of type datetime-local. The form is uploaded using Angular, and I need to convert the local date input to UTC before posting it. Despite trying different methods, I have been unable to resolve t ...

Finding the midpoint of the plotted GeoJSON route using Leaflet

I am currently working on a project that involves showcasing a map centered on a specific country and its neighboring countries using GeoJSON. I am curious if there is a way to determine the center point of only the visible portions of a country? One aspe ...

Introducing the First Angular Factory

It's high time for me to finally inject my first angular Factory . . . Here is the code I have: .factory('Debts', function($q, $scope){ return MA; }) .controller('Admin', function ($scope, Debts) { $scope.Debts = Deb ...

"Is it possible to differentiate between a variable that is BehaviorSubject and one that is not

I am dealing with a variable that can be of type Date or BehaviorSubject<Date | null>. My concern is figuring out how to determine whether the variable is a BehaviorSubject or not. Can you help me with this? ...

Creating a bot with discord.js to dynamically edit embed messages

While using discord.js version 12+, I encountered an error when trying to edit an embed sent by the bot. The error message displayed is as follows: Uncaught Promise Error: DiscordAPIError: Cannot edit a message authored by another user at RequestHand ...

Utilize JavaScript to restrict interactions to touch events in HTML

I am using JavaScript to create an HTML5 game where players can buy items using buttons. Unfortunately, I am unable to use ontouchstart='function()' for my buttons. Instead, I am utilizing the touchstart event listener and waiting for users to ta ...

What are the steps to implement timer control in Ajax using JavaScript?

Hello, I have been trying to establish communication with the server through my application. To achieve this, I made a call to the webservice using AJAX. Now, I am looking to incorporate a time interval in AJAX. Can anyone kindly provide guidance on how ...

What is the best way to populate a remote html page in real-time according to user input?

Is it feasible to use only JavaScript and HTML to allow users to select from a list of options and then print a page that includes a checklist of their selections? ...

RetrieveByUserIdentifier as a callback method (express)

Can you help me refactor the code below to use a callback function instead? I want to ensure that the Req and Res logic is handled separately. Userservice.js function getByUserId(req, res, next) { let userIDD = req.body.userID; User.findOne({ use ...

Does a legitimate array monad transformer exist?

I have successfully implemented the single linked list monad transformer, however, I am struggling to get its array counterpart functioning properly. The issue stems from a grouping effect that restricts the transformer's validity to commutative base ...

'hpack.js' dependency not found while using webpack-dev-server

Embarking on a new project, I took the initiative to update webpack and webpack-dev-server. I made sure to include the webpack-dev-server command in my start script within the package.json file. Strangely, the development server was up and running smoothly ...

Stop users from inputting dates beyond the current date in Angular 4

Encountering an issue with comparing the date of birth object and today's date object using Moment.js. Even if the entered date is smaller than today's date, it still throws an error. Below is the HTML code: <div class="form-group datepicker ...

What is the protocol for managing this exception? (specifically when a throw occurs within a callback function nested inside

Exploring the intricacies of exception handling and promises in Node.js is the focus of this query. By dissecting a simplified snippet of code, I aim to discern the similarities and differences between how exceptions are managed and promise/async functions ...

Comparison of valueChanges between ReactiveForms in the dom and component级主动形

Is there a method to determine if the change in valueChanges for a FormControl was initiated by the dom or the component itself? I have a scenario where I need to execute stuff() when the user modifies the value, but I want to avoid executing it if the v ...

guide to importing svg file with absolute path

I have been attempting to load SVG files from my LocalDrive using an absolute path. Despite successfully achieving this with a relative path, the same method does not work when utilizing an absolute path. <script> $(document).ready(functio ...

Bootstrap's square-shaped columns

I would like to implement a grid of squares for navigation purposes. By squares, I mean that the colored areas should have equal width and height. Currently, I have achieved this using JavaScript, but I am interested in a CSS-only solution. My project is ...

Struggling to incorporate typeWriter.js into the code with no luck

I'm attempting to make this script work. The script is designed to be referenced from an external script file, but I need it to be embedded for certain reasons which I won't delve into. I believe I've set it up correctly, but it seems that w ...