Tips for transferring a dynamic string array from JavaScript to a JSON object

function createNew(mouseEventData) {
 var measurementData = {
            visible: true,
            active: true,
            toolType: 'target',
            description: output
        };

        return measurementData;
}

 function onImageRendered(e, eventData) {
    var textline = ["Test"];
    var obj = { "description":textline};
    alert(obj)//[object Object]
    var output = JSON.stringify(obj);
    alert("output"+output); //output{"description":["Test"]}
}

In the example above, I am seeking assistance on how to dynamically pass a string array (textline) to the 'description' field of measurementData as a JSON object. Any help on achieving this would be greatly appreciated.

Answer №1

One possible solution is to utilize a global variable:

var dataInfo;

function generateNew(mouseData) {
    return dataInfo;
}

function onImageDisplay(e, displayData) {
    var lineText = ["Demo"];
    dataInfo = {
        visible: true,
        active: true,
        toolType: 'focus',
        details: lineText
    };
}

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

Show only the items in bootstrap-vue b-table when a filter is actively applied

How can I set my bootstrap-vue b-table to only show items when a filter is applied by the user (i.e., entered a value into the input)? For example, if "filteredItems" doesn't exist, then display nothing? This is primarily to prevent rendering all rows ...

What are the steps to create a Node.js application and publish it on a local LAN without using Nodemon?

When working on a Node.js application, I often use the following commands to build and serve it locally: //package.json "build": "react-scripts build", To serve it on my local LAN, I typically use: serve -s build However, I have been wondering how I ...

Why is the toggle list not functioning properly following the JSON data load?

I attempted to create a color system management, but as a beginner, I find it quite challenging! My issue is: When I load my HTML page, everything works fine. However, when I click on the "li" element to load JSON, all my toggle elements stop working!!! ...

LoopBack: Defining Mandatory Fields at a Future Moment

I encountered a situation where I need the 'email' property to be mandatory if the 'type' is set to 'online'. Generally speaking, I have a field that may or may not be required based on the value of another field. How can I ta ...

Arrange objects in an array based on certain criteria using JavaScript in a dynamic

I have an array containing various items that need to be sorted according to specific rules. The goal is to group all values with "rules" together, and ensure that "ELIG_DATABASE" is grouped with "ELIG_SERVICE." const items =[{"name":"ELIG_ ...

The data type JSON is not one of the standard JDBC types

JSON has become a widely used database type across all major SQL databases. I find it puzzling that there is no default JDBC type like java.sql.Types.JSON. ...

Issue: Accessing views on Android app can only be done by the thread that originally created the view hierarchy

I have successfully implemented an activity that populates a ListView in my Android application. Now, I would like to populate the ListView with JSON data fetched from a PHP script. I have created an adapter for my objects (Locations) and intend to add a n ...

Issue with Calendar Control not loading in Internet Explorer 9 when using ASP

I have been trying to incorporate a calendar control in my code that selects a date and returns it to a text field. It worked perfectly fine on browsers prior to IE 8, but I'm facing issues with IE 9. Can someone help me troubleshoot this problem and ...

Displaying a relative div on mouse hover over an HTML tag using React

In the section below, you will find the code snippet... <ul className="no-style board__list"> {Object.keys(today.books).map(function(id) { var refBook = today.books[id][0]; return ( ...

Shade the div if the name of the child element matches the location's hash

I am currently working on an AngularJS web project. My goal is to highlight a specific div when clicking on an anchor link. The structure of the HTML is as follows: <div interaction-list-item="" sfinx-interaction="interaction" class="ng-isolate-scope" ...

Modifying the cursor appearance during object dragging is a simple task that can enhance

html .container, .container * { cursor: url('img/arrowPointer.png'), auto; } javascript $('html').on('dragstart', function () { $('html').addClass('container'); }); $('html').on('d ...

Unsuccessful outcome from using a basic Ajax call alongside a result handler

Here is the JavaScript code: <script> jQuery(document).ready(function(){ // ensure DOM is ready jQuery('#veranstort').change(function(){ // trigger when input changes origin = "55767 Schwollen"; destination = "13509 Be ...

PHP is capable of showing echo statements from the function, however it does not directly showcase database information

My current challenge involves using AJAX to pass the ID name of a div as a string in a database query. Despite being able to display a basic text echo from my function, I'm unable to retrieve any content related to the database. // head HTML (AJAX) $( ...

Is there a framework available to animate Pseudo CSS elements?

Recently, I was working on developing a bar chart that utilized pseudo CSS elements (::before, ::after). While I successfully created bars that look visually appealing, I encountered a challenge when attempting to animate the height changes. Whenever I us ...

Tips for setting up a cross-application messaging service using Angular JS

I am looking to implement a messaging system for end users similar to Google, where messages are displayed at the top center of the web panel. Instead of adding HTML and related scripts to every form, list, and chart individually, I want to create a centr ...

Copy the CSS path from Firebug and paste it into the command line suggested

Currently, I am working on customizing the firebug plugin for Firefox. My goal is to enable a feature where when a user Inspects an element and clicks 'Copy CSS path', the CSS path will automatically be pasted onto the command line of the firebug ...

What is the most concise, stylish, and effective method in Javascript to move the last element of an array to the front?

I've been tasked with creating a Snake game https://i.sstatic.net/KXwNK.png and the logic for moving the snake is to rearrange elements in a JavaScript array. var links = [elem_0, elem_1, ..., elem_n]; To move the snake, I need to pop out elem_n, ...

Transform the code within a variable by utilizing JQuery. Delete the specific attribute and enclose it within a div

I am facing an issue where I have HTML code with a variable that I try to modify and apply changes to, but the code remains unchanged after modification. Basically, I have a piece of code containing a table and I want to remove an attribute and wrap it in ...

What is the best way to access and extract both the key and value objects in a Java Map using Jackson annotations?

I've been encountering a problem with serialization. I have a class called PickupDate with a LocalDate property and I want to serialize a Map<PickupDate, Integer> object to: { "2020-07-17": 1 } But instead, it's currently serializing to: ...

Exploring the inner workings of a JSON with Ajax and PHP

I'm working on a project that involves a JSON file called items.json. My goal is to create a search box where users can input either the name of an item or its category, and see live search results as they type. Here's what I’m aiming for: If ...