What is the best way to link a variable to a specific property within a

I am facing an issue with adding data from the server to a JSON property and displaying it. I know that JSON only accepts primitive types, so how can I dynamically add data to a JSON property? For instance, in the code snippet below, I am trying to assign the value of diagramName to the "text" property.

ctrl.js

 var diagramName = "";
$scope.data = resp.data;
angular.forEach($scope.data, function(diagram) {
    diagramName = JSON.stringify(diagram.name);
    console.log(diagram.name);
});

     [{
               "id": 1,
               "text": "My Folder",
               "children": [{
                   "id": 10,
                   "owner": "John Smith",
                   "text": diagramName,
                   "string": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>}]
               }]

Answer №1

Include the dynamic value in the attribute, prior to converting it into a string:

angular.forEach($scope.data, function(diagram) {
  diagram.text = diagramName
  diagramName = JSON.stringify(diagram.name);
});

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

"Refine Your Grid with a Pinterest-Inspired

I've set up a grid of images in columns similar to Pinterest that I would like to filter. The images vary in height but all have the same width. The issue arises when a taller image is followed by a shorter one, causing the short image to float right ...

Encountering difficulties in resolving the HTML view with Spring Boot

After creating a Spring Starter Project in Eclipse, I only selected the Web dependencies checkbox. Despite writing a simple html file and controller, I keep encountering the Whitelabel Error Page. I have made the decision not to use any template engines ri ...

What causes the discrepancy in results between the quoted printable encoding operation in JavaScript and Oracle?

Programming Languages: // JavaScript code snippet //https://www.npmjs.com/package/utf8 //https://github.com/mathiasbynens/quoted-printable par_comment_qoted = quotedPrintable.encode(utf8.encode('test ąčęė')); console.log('par_comment_qot ...

Utilize alternating colors from the Material UI palette to enhance the appearance of

Can someone help me understand how to utilize the different color variants in Material UI? For instance, the theme primary color has various options like 100, 200, 300, 400, 500, and so on. How can I access these colors from within my component? I attempt ...

Triggering event within the componentDidUpdate lifecycle method

Here is the code snippet that I am working with: handleValidate = (value: string, e: React.ChangeEvent<HTMLTextAreaElement>) => { const { onValueChange } = this.props; const errorMessage = this.validateJsonSchema(value); if (errorMessage == null ...

`Vue Component failing to display data from Blade File`

In my project using Laravel Blade, I am currently in the process of converting some blade files to Vue components. One challenge I encountered is trying to display a dynamically created page title on the screen from the Vue component rather than the blade ...

Having difficulty transforming a JSON string into a JSON object using Javascript

Currently, I am working on a hybrid mobile application using Angular. I have a string that is obtained from a $http.jsonp request and I am attempting to convert the response into JSON format. After checking the validity of the JSON at , it appears to be va ...

Transitioning the image from one point to another

I am currently working on a unique single-page website and I have been experimenting with creating dynamic background animations that change as the user scrolls. Imagine my website is divided into four different sections, each with its own distinct backgro ...

Achieve video lazy loading in JavaScript or jQuery without relying on any external plugins

I've been experimenting with lazy loading videos using jQuery. While I've had success with lazy loading images and background-images using the same technique, I ran into issues when trying to apply it to videos. Here's what I've tried s ...

Is there a way to set a personalized callback function when closing a modal with MicroModal?

I've been utilizing MicroModal for showcasing a modal window. Everything seems to be working smoothly, except for when I try to trigger an event upon closing the modal. It's unclear to me where exactly I should implement this callback function. ...

Having trouble transmitting JSON data with Durandal JS and Knockout Binding

var newData = { a: ko.observable(), b: ko.observable(), c: ko.observable(), d: ko.observable() }; function setupControlEvents() { $("#save").on("click", handleSave); } function handleSave() { var dataToSen ...

I must break free! Angular's $sce is failing to manage links to audio files (Strict Contextual Escaping)

I have successfully implemented Angular and $sce in my project to handle HTML special characters and link video files in the database to a video player. However, I am facing issues connecting the HTML audio player to audio files stored in the same database ...

Attempting to access the nested JSONObject within another JSONObject

I have this JSON I generated: { "error":false, "0":{ "tvInfo":{ "id":"0", "name":"The War of Thrones", "type_id":"1", "score":"8.1", "votes":"780", "created_date":"2011-04-17", ...

Tips on concealing a single row within an ng-repeat without altering the $index value

This code block shows a list of users using ng-repeat. <tr ng-repeat="userList in userLists" ng-if="userLists.role!='admin'"> <td data-th="S.no">{{$index+1}}</td> <td data-th="Role">{{userList.role}}</td ...

`Zooming and scrolling feature within a masked image`

I'm struggling to achieve a scrolling zoom effect similar to the website mentioned below, but I can't seem to get it to fully zoom. Additionally, when I try to zoom in on a clipped shape or text while scrolling, the entire div ends up scrolling ...

Horizontal navigation bar encroaching on slideshow graphics

I'm encountering difficulties aligning the horizontal menu below the image slider. When I have just an image without the slider, the menu aligns properly (vertically), but as soon as I introduce the code for the slider, the menu moves to the top and d ...

What is the best way to start and display an animation for a div element?

Hey all! I'm trying to create a menu div layer that opens when a button is clicked and closes on mouseover using Angular. I want the div to start partially closed and then fully open on click, but my code isn't working as expected. Here's wh ...

Importing modules dynamically in NextJS allows for the loading of

I have a basic function that is responsible for loading a script: const creditCardScript = ( onReadyCB, onErrorCB, ) => { let script = document.createElement("script"); script.type = "text/javascript"; script.src = process.CREDIT_CARD_SCRIPT; ...

Tips for Retrieving Data from a Multi-Dimensional Array

I need help accessing the values in my array and assigning them to variables for later use. I have created an array and used the randomGo() function to generate a random number that corresponds to a pair of numbers within the array. My goal is to assign ...

unable to maintain session in express-session for mobile web application

My web app is created using NodeJS, Express, and AngularJS. I have implemented express-session for managing login sessions. While the login session persists in desktop browsers even after closing the browser, mobile users are facing an issue where they ge ...