Change an array into JSON format using the extJS library

Within my extJS form, I am utilizing a multiselect combobox. Upon submission, it provides an array of strings.

I am looking to convert this array into JSON format in a specific way. As an example, the initial array may look like this:

categories : ['ABC','XYZ']

The desired JSON format would be:

"categories":[{"name":"ABC"},{"name":"XYZ"}]

Are there any built-in methods in ExtJS to achieve this conversion? Alternatively, how can this be accomplished using JavaScript?

Answer №1

To achieve this, you can utilize the Array.map function in plain JavaScript:

var result = JSON.stringify(categories.map(function (item) {
  return { title: item };
}));

Result

[{"title":"Apple"},{"title":"Banana"}]

Example

Answer №2

What is the reasoning behind not utilizing

selectedList = [];
Ext.each(categories, function (category) {

   selectedList.push({
          'name': category
   });
});

Ext.encode(selectedList)

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 test an external Data Transfer Object (DTO

I am looking to test an external Data Transfer Object (DTO) that undergoes frequent changes. For example: Here is a sample JavaScript (JSON) file below: // JavaScript const type User = { id: Number, name: String } // JSON user: { id: Number, ...

Strategies for smoothly navigating the page to a specific div every time

Currently, I am working on a form that requires submitting multiple child forms. To enhance user experience, I have incorporated jQuery functionality to display a message at the top of the page upon each submission. Now, my goal is to implement a feature w ...

Attempting to insert an empty <option> into a dropdown menu using jQuery and ajax functionality

I'm working on a JavaScript function that dynamically populates a select dropdown based on the value of another select dropdown. I want to include an empty option at the beginning. Here is the code snippet for the function: function createParkFloorM ...

Disable the form after submission using Bootstrap 5

How can I submit a form using the Post method in Bootstrap v5 and then indicate that the form is being processed? I've encountered numerous examples for Bootstrap v4 and jQuery, but since Bootstrap v5 no longer supports jQuery and there are potential ...

Can you confirm the mobile type, please? Using JavaScript to display a div only once based on the mobile type

Is there a correct way to determine the type of mobile device I'm using? Are there alternative methods to check for the mobile type? Take a look at my approach in the code below. How can I test this using a tool? Does anyone have insights on checki ...

What is the best way to send the 'FIN' in node.js?

Update: After meticulously adding all possible options one by one, I still encountered a 120-second timeout. Quite perplexing. Upon further investigation, it appears that on my Windows 7 machine, it consistently takes exactly 120 seconds (or rather, 122 s ...

Creating a JsonPath from a soapui Json Response with groovy: A step-by-step guide

Upon receiving a SOAPUI response, I attempted to parse the JSON and display all elements (from leaf nodes) in the response. The sample JSON is as follows: Sample Json : { "BookID": 7982, "author": { "authorname&quo ...

Learn how to incorporate an image into your Codepen project using Dropbox and Javascript, along with a step-by-step guide on having the picture's name announced when shown on the screen

Hey there, I'm in need of some assistance. I have a Codepen page where I am working on displaying 4 random shapes (Square, Triangle, Circle, and Cross) one at a time, with the computer speaking the name of each shape when clicked. I want to pull these ...

Navigating on Blogger can be a tricky task when it comes to searching and

I recently added a Table to my blogger post, along with an input form for readers to easily search data. After finding the code on W3Schools (link here), I implemented it successfully. However, I am looking to make some improvements. Here is my question: ...

Optimal techniques for parsing JSON data from a URL and saving it to an SD card for offline accessibility

In my applications, I'm using GSON to parse JSON feeds. I followed a tutorial and used a specific code to make it work as intended: InputStream source = retrieveStream(url); Gson gson = new Gson(); Reader reader = new InputStreamReader(source); //* ...

Error Checking in AngularJS Form Submission

According to my form.json file, I have a form that needs validation and a simulated submission. Firstly, I need to address this issue: fnPtr is not a function Next, I want to submit the form to a mocked API endpoint that will return true or false. Can I ...

Employing on() for triggering a form submission

I am attempting to attach a submit event handler to a form that may not always be present in the DOM, so I am using .on(): $('body').on("form","submit", function(e){}) However, when checking Firebug, it shows: $("body").on is not a function ...

managing pictures with ng-repeat

I am struggling to display images from an array using ng-repeat. Can someone help me with this? var images = [ "http://35.154/media?request={"request":{"service":{"servicetype":"6","functiontype":"1013","session_id":966},"data":{"mediaids":171}}}", "http: ...

Ways to implement a delay in a function?

I'm looking for a way to introduce a delay in my function. Should I enclose the function within a delay function, or is there a different method to ensure that the animation doesn't trigger until 5 seconds after the page has loaded? var textTo ...

Overuse of backslashes in JSON data for URL requests

For my URL request, I need to send data in JSON format. Below is the code snippet with an input parameter num: #* @get /getComm getComm <- function(num=1){ library(jsonlite) #some computation here lst<-list(links=linksff,nodes=sc,directed=FALSE,mul ...

div value causing erratic behavior in cycling process

Instead of following the normal balance calculation (1000-950), something odd happens and the balance goes from 1000 to 0 with the while loop. I never intended for the refBalance to drop below 0. (numDebit2() returns 50) <div class="elemen ...

Can the same form be submitted with two different actions?

I am facing an issue with a form that is supposed to submit data to 2 different pages using the POST method. After trying some javascript code, I found that one form submission works correctly while the other does not. <form id="add"> <input ...

Using Swift to populate a TableView with JSON data

Hello everyone, I am completely new to Swift and in need of some assistance. I am facing an issue with loading three different JSON files at different times. The first JSON file loads perfectly fine, but when I click on an item to reload another JSON file ...

Display only the initial outcome when using array_diff on an associative array

After realizing I forgot to ask the full question in a previous post, I'm glad I caught myself before asking the wrong one. So, here's the complete question: Array ( [idAsset] => 10000005 [AssetName] => HP ) Array ( [idAsset] => 1000000 ...

Utilizing an array in a PHP URL and incorporating it into JavaScript operations

In this PHP file, I am working on validating only numeric input for text-boxes with the ids "Mobile" and "Home": $elementids = array("Mobile","Home"); $serialized = rawurlencode(serialize($elementids)); $testvar='validate-nums.php?elementids='. ...