Attempting to transform a brief JavaScript script into Java code

There's a code snippet in JavaScript that I'm not familiar with, but I need to pass this information to one of my Java methods.

example.setChoices([{ value : 'One', label : 'Label One', disabled : true }], 'value', 'label', false);

I've been researching on w3schools and other JavaScript sources, but it doesn't seem like a simple map.

I'm struggling to understand what kind of Map this would be in Java.

My objective is to send this data to my Java method.

Answer №1

Although unconventional, it is possible to create a <Object,Object> map in JavaScript due to its flexibility with mixed datatypes.

One approach could be converting boolean values to strings for consistency within the "raw object map."

    Map<Object, Object> test = new HashMap<>();
    test.put("firstString", "first");
    test.put("secondString", "second");
    test.put("thirdBool", true);

In regards to the provided code:

Map<Object, Object> test = new HashMap<>();
test.put("value", "value");
test.put("label", "label");
test.put("disabled", false);

For custom classes, it's recommended to define a specific class structure like this:

class FooObject{
    private String value;
    private String label;
    private boolean disabled; 

    FooObject(String value,String label,boolean disabled){
        this.value=value;
        this.label=label;
        this.disabled=disabled;
    }

    public String getValue(){
        return this.value;
    }

    public String getLabel(){
        return this.label;
    }

    public boolean isDisabled(){
        return this.disabled;
    }
}

This custom class can then be used in a map as shown below:

Map<Integer, FooObject> test = new HashMap<>();
test.put(0,new FooObject("first","1 label",false));
//Accessing parameters
test.get(0).isDisabled();
test.get(0).getValue();
test.get(0).getLabel();

While Java does not support default values, you can override constructors to simulate defaults:

FooObject(String value,String label,boolean disabled){
    this.value=value;
    this.label=label;
    this.disabled=disabled;
}

FooObject(String value,String label){
    this.value=value;
    this.label=label;
    //default value
    this.disabled=true;
}

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

The PHP page is not receiving the variable passed through AJAX

Within the following code snippet, there seems to be an issue with accessing the dataString variable in the comment.php page. To retrieve the variable name, I utilized $_POST['name']. $(document).ready(function(){ $("#submit").click( function() ...

A Java spider for collecting both first and third-party cookies

My goal is to develop a Java crawler that can extract all cookies from a website. This crawler should be able to automatically crawl through a list of websites and their subpages. To achieve this, I have utilized jSoup and Selenium in my implementation. ...

Using Angular to create HTTP routes in conjunction with Node.js

My current challenge involves trying to access a .json file that contains my portfolio. I have set up my backend using express js, and am attempting to retrieve the data using angular in the following manner: $http.get("data/items.json") .success(function ...

Oh no, it looks like Maven encountered an error while trying to execute a goal on the project. Has Hibernate 5 disappeared from

Out of nowhere, I encountered an error while trying to build my Maven project. Here's the stack trace that was generated: [ERROR] Failed to execute goal on project repgen: Could not resolve dependencies for project ee.estcard.repgen:repgen:jar:0.1: ...

What is the best way to ensure the options/choices div reaches its ideal width?

Check out my StackBlitz project that I've set up In the styles.css file, you'll notice that I defined a fixed width of 650px for the option dropdown div, which is currently working fine @import '~bootstrap/dist/css/bootstrap.min.css'; ...

Creating space between flex items in Slick Carousel with Vue

This is my current Slick Carousel. There are 4 items in a row, and I am looking to insert some margin between each item while maintaining the existing aspect-ratio: 1.3/1; I'm struggling with overriding the classes from vue-slick-carousel. Does any ...

Display the chosen alternative in a Bootstrap dropdown menu

I am currently facing an issue with the dropdown list in my bootstrap menu. <li class="dropdown"> <a aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">Chose option<span class="c ...

Finding it difficult to comprehend how interfaces work in Java

I recently defined some interfaces: public interface A { public void print(); } and another interface: public interface B extends A { public void print(); } After that, I created a class: public class C implements A,B { @Override publ ...

The transformation of all relative URLs into absolute URLs is carried out

The issue at hand seems to be peculiar to Chrome and Firefox, as IE does not exhibit the same behavior. In my angular application, I've noticed a strange phenomenon. Once a specific view is loaded, all XHR requests made using relative paths are autom ...

Issue encountered while establishing a connection to the database using the Spring framework

I am currently working on setting up a CRUD application using a basic mySQL database within the Spring MVC framework. However, I am encountering difficulties when trying to establish a connection to the database. The specific error I am receiving is as fo ...

Troubleshooting problem with local storage: JSON/JQuery items not appearing on display

My goal is to retrieve and organize all the items in localStorage and display them on an HTML page. Here is my approach: <script> function ShoppingCart() { var totalPrice = 0; var output; var productName; var productAlbum; var ...

Issues with sending data through ajax using the post method persist on shared web hosting

I'm facing an issue with Ajax post data. It works fine on localhost but not on shared hosting. Here is my code: <script type="text/javascript> $(document).ready(function(){ $("#login").click(function(){ alert(& ...

Utilizing keyboard shortcuts for webpage navigation (within a content script)

Just to clarify, I'm not looking for code assistance right now. I mostly need a starting point. My goal is to create a Chrome browser extension that enables me to use keyboard keys for navigation on a specific webpage. The page in question belongs t ...

When an input is disabled in "react-hook-form", it may return undefined

Within my React application, there exists a form containing various input fields. I have enclosed these fields using FormProvider imported from react-hook-form and utilized register within each field. import { useForm, FormProvider, useFormContext } from & ...

Enclose chosen images with selection made

Below is the code snippet I am working with: <select multiple="multiple" class="image-picker show-html"> <option data-img-src="http://placehold.it/125x200" value="1">SportField 1</option> <option data-img-src="http://placehold ...

Resolving a Routing Problem with moment.js

Hi everyone, I'm new to working with express.js and backend routing. I'm encountering an error with my server code and would appreciate any insights on what might be causing it. I've already tried using res.end() with plain text, but the err ...

Update the content within a div based on the selected option from a dropdown menu or

Is there a way to change the displayed text based on user input or selected option? By default, the text shown is "Aa Bb Cc Dd Ee...", but it can be changed by selecting different options. If text is typed into the input field, the displayed text will up ...

Display a thumbnail image using a v-for loop

Looking for help with implementing a photo preview in my code using BootstrapVue. The Vue devtools show that the form-file contains the image, but my 'watch' isn't functioning properly. Any assistance would be greatly appreciated! Here is ...

What is the process for modifying event (hover & click) on a legend item within highcharts?

When hovering over chart points, you can see the point value in the center of the pie chart. Similarly, when you stop hovering over a chart point, you can see the total value displayed. This behavior also applies when hovering over a legend item. const cha ...

I am encountering issues with the text box summing as it is providing me with

Hey everyone, I hope you're all having a good morning. I'm facing an issue with 4 text boxes that should be summed up and the total displayed in 1 text box. However, instead of getting the correct total value, I seem to be getting something else ...