Tips on displaying information following the parsing of JSON

Trying to retrieve a list of users using this code snippet

<div id="users" data-users='[{"name":"one","userName":"user_one"}, 
{"name":"two","userName":"user_two"},{"name":"three","userName":"user_three"}]'></div>

Seeking assistance on how to load the list of users within the values section.

const users = document.querySelector("#users");
const json = JSON.parse(users.dataset.users);
var tribute = new Tribute({
 values: ** Insert user loading logic here **
});

Answer №1

It appears that you are assigning the outcome of JSON.parse(users.dataset.users) to a variable named "json". This raises questions about your understanding of the output generated by JSON.parse.

The data-set attribute on the div currently holds json as its value, so invoking document.querySelector("#users") will return the json string.

By using JSON.parse(users.dataset.users), you will convert the json string (users.dataset.users) into a JavaScript object, specifically an array of users which I assume is intended for assignment to the values property within the Tribute constructor.

To clarify this process, I have modified your variable names in the code snippet below:

const json = document.querySelector("#users");
const users = JSON.parse(json.dataset.users);
let tribute = new Tribute({ values: users });

* As mentioned by "the_previ," it remains ambiguous without defining Tribute what type of data the "values" property expects (e.g., String, Number, Array). My assumption is that you aim to pass in the array of users.

Answer №2

To simplify this process, you can leverage the power of Lodash! All it takes is importing Lodash and utilizing the map function like so:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>

<script>
const users = document.querySelector("#users");
var json = JSON.parse(users.dataset.users);

const userlist = (_.map(json, "name"));
</script>

The resulting userlist will store all the "name" values in an array.

If you prefer using userName values instead, simply substitute name with userName within the map function!

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

Error: AppModule requires an array of arguments in order to function properly

Upon successfully compiling my Angular application and running ng serve, I encountered the following error in the browser console. AppComponent_Host.ngfactory.js? [sm]:1 ERROR Error: Arguments array must have arguments. at injectArgs (core.js:1412) at c ...

emphasizing the specific text being searched for within the page

Looking for a solution to search values and highlight matching text words on a page? Let me help you with that. $(function() { var tabLinks = $('.nav > li'), tabsContent = $('.tab-content > div'), ...

Inserting a new object for the key "iOS" into an NSMutableArray of dictionaries sourced from a

After receiving an array of dictionaries from a JSON feed, I assign it to an NSMutableArray named jsonArray as shown below: jsonArray = [deserializedData objectForKey:@"reports"]; The structure of the feed is as follows: reports = ( { ...

What is the best way to send an array to php from a python script?

Utilizing a Python script within my PHP code has proved to be quite beneficial: <?php $command = escapeshellcmd('<path>/myscript.py'); $output = shell_exec($command); echo $output; ?> However, the current challenge I am facing inv ...

Is it possible to utilize a map in Python or incorporate other advanced functions to efficiently manage indices?

When working with higher order functions in JavaScript, we have the ability to access the element, index, and iterable that the function is being performed on. An example of this would be: [10,20,30].map(function(element, index, array) { return elem + 2 ...

Using Express for Managing Subdomains, Redirects, and Hosting Static Files

I am struggling to configure Express in a specific way and can't seem to get it right. Despite researching on various platforms like SO, I still can't figure it out. Hopefully, by explaining my intentions here, someone can guide me in the right d ...

Managing user sessions in Node.js

What is the best way to manage SESSIONS in Node.js? Specifically, I am interested in storing a UserID in a session using Node.js. How can this be accomplished, and is it possible to access that Node.js session in PHP as well? I am looking for an equivale ...

Tips for triggering a sound only when transitioning from a true to false value for the first time

I have data for individuals that includes a dynamically changing boolean value. This value can be true or false, and it updates automatically. The webpage fetches the data every 5 seconds and displays it. If the value for any person is false, a sound is p ...

What is preventing me from generating a string for transform:translate within Angular.js?

Attempting a new approach here $scope.graph.transform = "transform: translate(" + $scope.graph.width + "," + $scope.graph.height + ");"; Despite my efforts <h3>transform: <span ng-bind="grap ...

The JavaScript function on the specified /url page is not functioning properly following the execution of history.push(/url) on click

I have a JavaScript function that toggles the display of login password. However, when I redirect to the login page from another page using history.push(/login), the function does not work. It does work when I use (/login) in the href tag. How can I resolv ...

Issue with rendering in React Router

I've been having trouble with React Router in my code. I know that there have been changes from Switch to Routes in React Router Dom v6, but even after making the adjustment, my program just displays a blank screen. Can anyone help me resolve this iss ...

Using JS/AJAX to update a section of a webpage when a button is clicked

Currently, I am working on developing a generator that will display a random line from a .txt file. Everything is going smoothly so far, but I have encountered an issue - I need a specific part of the page to refresh and display a new random line when a ...

What is the best way to set up v-models for complex arrays or nested objects?

I'm looking to efficiently create multiple v-models for random properties within a deep array, where the position of attributes in arrays/objects can change dynamically. While I've managed to achieve my goal with the current setup, I'll need ...

gather remarks published by a Facebook Page

Is there a way to retrieve the comments made by a Facebook page, such as this one? The code provided only fetches general information. What format should the URL be to retrieve the comments specifically? $Page_ID = 'https://www.facebook.com/nauhote ...

Smooth scrolling feature malfunctioning in mobile view

While working on my website, I noticed that the smooth-scroll feature works perfectly on desktop browsers. However, on mobile devices, when I click on a link, it does not scroll to the correct position. It always ends up much lower than expected. Any idea ...

Is it possible to hide a div using Media Queries until the screen size becomes small enough?

Whenever the screen size shrinks, my navbar sections start getting closer together until they eventually disappear. I've implemented a button for a dropdown menu to replace the navbar links, but I can't seem to make it visible only on screens wit ...

Incorporating dynamic images into Laravel using Angular

I am currently trying to dynamically input the src value of an image using Angular. Below is the code I have written : HTML : <div ng-app="myApp" ng-controller="MyCtrl"> <img src="{{asset('assets/img/'.'/'. @{{ item.name ...

Navigate the conversation within the dialog without affecting the content below

Is it possible to have a dialog with scrollable content on top of a page that is also scrollable, but when trying to scroll inside the dialog using the mouse wheel, only the dialog body scrolls and not the page below it? What approach can be used to accom ...

Troubleshooting a jQuery carousel: How can I prevent the thumbnails from automatically moving forward?

I am currently modifying a carousel where the thumbnails are moving out of frame. I need assistance in keeping them stationary, except for the blue thumbnail highlighter. To better illustrate the issue, I have created a jsFiddle here: http://jsfiddle.net ...