Search code examples
javascriptarraysjsonextjs4

Convert array to JSON in extJS


I have a multiselect combobox in my extJS form.

It returns an array of strings on submit.

I want to convert it the array to json(in a particular format).

Eg. It returns in the following array:

categories : ['ABC','XYZ']

I want the JSON in the following format:

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

Are there any methods in ExtJs to do this? How to achieve this using javascript?


Solution

  • You can use the Array.map method in vanilla JS:

    var out = JSON.stringify(categories.map(function (el) {
      return { name: el };
    }));
    

    Output

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

    Demo