Transferring data from a .NET array to JavaScript

I have gone through numerous samples and suggestions, but unfortunately, none of them seem to work effectively in my case.

When using winForms webControl, I am attempting to send an array of addresses to the Google Maps API for stops along the way.

Everything functions perfectly without the stops array. Below are snippets of the code:

JavaScript:

 function calcRoute(origin,destination, way ) 
 {
 var waypts = [];

 for (var i = 0; i < way.length; i++) {
             waypts.push({
              location:way[i],
              stopover:true});}
.....

VB.net

   Private Sub GetDirections_Click(sender As Object, e As EventArgs) 
        Dim origin As String = "1 Main St"
        Dim destination As String = "200 Main St"
        Dim wayP = New System.Web.Script.Serialization.JavaScriptSerializer().Serialize({"123Main St.", "189 Main St"})
        InvokeScript("calcRoute", origin, destination, wayP)
    End Sub

    Private Function InvokeScript(name As String, ParamArray args As Object()) As Object
        Return WebBrowser1.Document.InvokeScript(name, args)
    End Function

EDIT: In JavaScript, the desired output should be:

        [{
          location:"10201"
        },
        {
          location:"10202"
        }]

Answer №1

When putting in an array of strings, the desired outcome is an array of objects. To achieve this, a basic class with a location property must be created to then serialize an array of these objects.

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

What is the best way to dynamically hide a textbox in JSP based on the selection of a

On my JSP page, I have a textbox and a checkbox. I attempted to use jQuery and JavaScript to hide the textbox when the checkbox is checked, but it doesn't seem to be working. Below is the code snippet: <p class="contact"> <input id="check" n ...

Transform the appearance of a button when focused by utilizing CSS exclusively or altering it dynamically

As a newcomer to coding, I am currently working on a project that involves changing the color of buttons when they are clicked. Specifically, I want it so that when one button is clicked, its color changes, and when another button next to it is clicked, th ...

Mongoose - Mastering the Art of Executing Multiple Update Statements in a Single Operation

In the MongoDB documentation, I found out that you can execute multiple update statements in a single command. How can this be accomplished with Node.js and Mongoose? db.runCommand({ update: <collection>, updates: [ { q: <q ...

I am looking to trigger the change event from within the click event in Angular

My objective involves detecting when the cancel button is clicked while a file is being uploaded. I am trying to accomplish this by triggering the click event, then monitoring for the change event. If the change event occurs, the document will be uploaded. ...

The imported path is not found in Tsconfig

Hey there! I've been working on getting my project's imports to play nice with typescript import paths. Every time I encounter this error : Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'app' imported from dist/index.js It seems l ...

Is there a way to remove the commas in between the array elements when printing out the Pascal triangle? I want to display it without any commas separating the values

user_height = int(input("Please enter the height of the triangle: \n")) triangle_array = [1] for i in range(user_height): print((str(triangle_array)[1:-1])) new_array = [] new_array.append(triangle_array[0]) for j in ra ...

"Utilize JavaScript to extract data from JSON and dynamically generate a

I'm currently facing an issue with reading JSON data and populating it in my HTML table. The function to load the JSON data is not working as expected, although the data typing function is functioning properly. I have shared my complete HTML code alo ...

Retrieve distinct values for the keys from an object array in JavaScript

Here is the structure of my array: const arr1 = [ { "Param1": "20", "Param2": ""8", "Param3": "11", "Param4": "4", "Param5": "18", ...

Tips for dividing a string without removing the split character

Is there a way to divide a String into segments without getting rid of the characters in between? For example, I want the following output: parts[0]= 4x parts[1]= -3y parts[2]= 6z parts[3]= 3v This is my approach: import java.util.Arrays; public class ...

The loading time for the Ajax request is unreasonably slow

I am currently managing a website dedicated to League of Legends. My main task involves requesting statistics from the Riot Games API based on a player's name, which returns the information in JSON format. However, there is a significant delay in load ...

- catalog of pictureIcon- compilation of photoIcon

Could it be feasible by utilizing a list such as private LinkedList<Object> deckOfCards = new LinkedList<Object>(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 13; j++) { deckOfCards.add(new Card(Rank.v ...

Unable to reach elements that have been loaded through ajax with jQuery

I am facing an issue where I cannot access content loaded via ajax for modification. Unfortunately, I do not have access to the js file responsible for the initial load. Therefore, I need to create a separate function to alter the content. The required mo ...

Validating data with Joi can result in multiple error messages being displayed for a single field

I'm attempting to implement a validation flow using the joi package, which can be found at https://www.npmjs.com/package/joi. 1) First, I want to check if the field category exists. If it doesn't, I should display the error message category requ ...

Ways to extract text from a temporary element

Here is my HTML code: <div class="p-login-info" ng-show="loggedOut()">My text.</div> This is the corresponding JavaScript code: var e = element(by.className('p-login-info')); e.getText() .then(function(text){ var logoutText = ...

Determine if there are any two numbers in a given list that combine to equal a specified number k

This particular question was posed during a rigorous Google programming interview. I brainstormed two different methods to solve it: The initial approach involves finding all subsequences of a certain length, computing the sum of their elements, and ch ...

Tips for implementing the App Tracking Transparency feature on Maui

How do I implement App Tracking Transparency (ATT) in my Maui app for iOS? I have added the following code to MainPage.xaml override void OnAppearing() { base.OnAppearing(); // Request user's tracking authorization ATTrackingManager.Requ ...

Verify the length of an array within an object using JavaScript

I am facing a problem with an object. Here is what it looks like: const array = { "entities": [ { "annexes": [ { "buildingUniqueIds": [] }, { ...

Rendering an image on the HTML5 canvas

I really want my image to be in the perfect spot. I already have this code set up, but how can I adjust the positioning of my image? Whether it's a little to the left or more to the right, I'm talking about the exact coordinates. Where do I need ...

Tips for correctly listing the elements of an object within a state array?

Recently, I've delved into the world of React and have been immersing myself in tutorials while also experimenting with my own projects. One question that has crossed my mind is how to best enumerate objects in a state array that each contain an ID n ...

Filtering Array Values within an Object using JavaScript

I need help filtering an object based on array values. For example: { "sun":["sleep","walk"], "mon":["read","dance","ride"], "tue":["work",&q ...