Incorporate an array into a JSON object using ExtJS

For my json object, I need to incorporate an attribute called idField. This idField should be structured as an array if it contains multiple elements, or as an object if it has only one element. I want to generate this idField from the data stored in an extjs store. Essentially, if the store holds just one element, I need to insert an 'object' for idField in the json object, but if there are multiple elements, then I need to insert an 'array'. Can you guide me on how to achieve this?

UPDATE:

The data in my store consists of 2 columns: 'name' and 'type'. If the store has only one row, I want the object structure to be like this:

{
    ...
    idField : {
        name : ...
        type : ...
    }
}

If there are 2 rows in the store, then the object structure should appear as follows :

{
    ...
    idField : [
    {
        name : ...
        type : ...
    },
    {
        name : ...
        type : ...
    }
    ]
}

It is important to note that the idField attribute needs to be inserted within the object.

UPDATE 2:

When I executed

console.log(Ext.getStore("My_store_name").data)
, I received this object in the console.

Answer №1

Extracting data from an EXT JS store can be done with the following code:

var data = Ext.getStore("Your_Store_Name").data 

The example below demonstrates how to retrieve values from a field named 'item':

var data = Ext.getStore("Your_Store_Name").data 

// Assuming the JSON data is stored in a variable called myJSON
if (data.items.length == 1){ 
    myJSON.idField = {type: 'sometype', name: data.item[0]} 
} else if (data.item.length > 1)   
{
    // Create an array of JSON objects
    var list = []; 
    
    for(var i = 0; i < data.item.length; i++)
    {
        list.push({type: 'sometype', name: data.item[i]})
    } 
    
    myJSON.idField = list; // Assign the array of 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

I have chosen to use a json file as the data source for the AutoComplete feature, as it allows for quicker loading times on the client side. This approach helps minimize hits to the database. Do you think

Currently, I am utilizing a JSON file as the source for AutoComplete. The JSON file is being downloaded at the client side in order to minimize hits on the database. I'm experimenting with this approach to find the most efficient way. What are your th ...

Utilizing Xcode to leverage JSON data and the POST method for verifying a user's credentials

Having recently started working with POST requests, I wrote a code snippet that collects a username and password input for validation purposes. The idea is simple - if the input is valid, it should return true, otherwise false. While most of my code seems ...

Widget Issue: Problem with Setting jQuery Datepicker Format to European Style

I could really use some assistance with this issue. My goal is to display the check-in and check-out dates in European format (dd/mm/yy) within the input field on this specific page: (username: 'numberone' ; pass: 'num270514'). When t ...

"Encountering an error with Meteor while attempting to generate ObjectID due to an invalid hexadecimal

Recently, I've been working on updating data in the database while also inserting new information using jQuery (I know, I'm still learning). After this process, I need to be able to click on the data and display some user interface elements, whic ...

Is it possible to implement a wsdl webservice in Phonegap?

Does anyone know the best way to call a wsdl webservice in Phonegap? Any suggestions on source code or tutorials that could be helpful? ...

Learn how to effectively transfer data from $.getjson to PHP, specifically through the use of Laravel's @foreach loop

$.getJSON( 'http://localhost/media_books/index.php/new_books.json?provider_id=1&limit=99&offset=1') .done(function( json ) { $(tar).closest('.accordion').children('div').text(json); }); I have successfully obtaine ...

Having trouble running Ajax with JavaScript on a Wamp server

Exploring the realm of ajax, I embarked on a journey guided by a YouTube tutorial to construct a basic food search application. The concept was simple - users would input the name of a food item, and if available, its name would be displayed below. On the ...

Issue with the scope of Bootstrap Accordion

Observing that the event triggers on a single Bootstrap accordion can affect all other accordions on the same page, I am wondering if there is a way to isolate this behavior without altering the markup or using $(this). Any suggestions? Check out this exam ...

Validating textfield input is crucial before sending a request to the server

I need assistance with my Android app registration form. I am encountering an issue where the setError feature is not preventing form submission to the server until all text fields are validated. Despite implementing checks, the form still submits data f ...

placeholder for dropdown selection in Vue.js version 2.0.0

Currently, I am in the process of developing a web application using vuejs 2.0. In order to create a straightforward select input, I employed the following code: <select v-model="age"> <option value="" disabled selected hidden>Select Age&l ...

Connecting to PostgresSql through Web Services

Currently, I am in the process of developing an iOS application and integrating a PostgreSql database. My goal is to establish web services that can communicate with the database for data retrieval and insertion purposes. For instance, I aim to set up a we ...

Set the variable after catching it

Within my AngularJS application, I am utilizing a $watchCollection function to call the getBalance(address) function within the listener. $scope.$watchCollection('settings', function() { for (i = 0; i < $scope.settings['accounts&ap ...

Is the textarea's shape out of the ordinary?

Most textareas are typically rectangular or square in shape, similar to this: However, I am interested in having a custom-shaped textarea, like the example below: Is it feasible to achieve? ...

Transform a Python list into JSON format by adding a designated key value

My python list looks like this: vehicles = ['car', 'bus'] I'm looking to convert it to JSON format in the following structure: [ { "vehicle": "car" }, { "vehicle": "bus" } ] I also attempted to convert it to a dictionary but di ...

Display checkboxes on all TableRow elements as soon as one of them is checked

I've incorporated a material-ui Table into my project and have successfully implemented multi-select functionality. Here are the requirements I've fulfilled so far: Checkboxes are initially hidden - COMPLETED Hovering over a row reveals its che ...

The serverSideTranslations function require an initial locale argument to be passed in order to properly set up localization data

Decided to post here as per the recommendation of next-18next developers. I'm encountering an issue with next-i18next after updating both i18next and nextjs. The error message states: Initial locale argument was not passed into serverSideTranslation ...

Experience a unique custom layout using AppRouter in Next.js version 13, with the added

My goal is to encapsulate the _app.js file within a custom layout, similar to what I have done before. This way, I can include a Navbar at the top and wrap everything else within a container: //layout.js file import { Container } from 'react-bootstrap ...

Obtaining numerous files in base64 along with their respective file names using FileReaderAPI in Javascript

When I upload 3 items named png1, png2, and png3, the result will be as follows: alert 1 png1 / base64 string conversion alert 2 png2 / base64 string conversion alert 3 png3 / base64 string conversion I experimented with this code snippet. fu ...

Issues presenting themselves with Material UI Icons

I need some help with a problem I'm having. I'm trying to integrate my React app into an Angular app, and everything is working fine except for the material-ui/icons. They appear, but they are not displaying correctly! I have made sure to use th ...

Having issues with Sequelize and SQLite auto increment functionality. No errors when using AutoIncrement, but the auto increment feature is not working. On the other hand, errors arise when using

I am currently in the process of developing a discord bot, which includes 2 slash commands. One command is called /setup, used for adding the guildId and an adminChannel to a database. The other command is named /onduty, which is used for adding the user, ...