retrieve the information from a specific field within the store and store it in an array

Perhaps this is a question that pertains to using pure JavaScript, but for some reason I can't seem to figure it out. I am currently working on extjs4.2 using Sencha Architect and have received a JSON response from the server in the following format:

{
    "data": [{
        "ExamID": 1,
        "ExamName": "Semester-1",
        "MaxMarks": 100
    }, {
        "ExamID": 4,
        "ExamName": "Test-1",
        "MaxMarks": 10
    }, {
        "ExamID": 5,
        "ExamName": "Test-2",
        "MaxMarks": 10
    }]
}

My goal is to reconfigure the grid using only the data from the "ExamName" field. Therefore, "ExamName" should be passed as an array in the reconfigure() function.

Unfortunately, I am struggling to retrieve "ExamName" in array form. Any assistance you can provide would be highly appreciated.

var gridStore = Ext.data.StoreManager.get('ClassSemesterStore');
var g = gridStore.load( {params : {ClassID: ClassData }});
var data = g.data;
var length = data.getCount();
var examName = [];
for(var i = 0; i < length; i++){
  examName.push(data[i]['ExamName']);
}

This is resulting in an error message stating "Uncaught TypeError: Cannot read property 'ExamName' of undefined".

Answer №1

It seems like your goal is to transform examName into an array that contains each ExamName from the response data. To achieve this, you can use the following code snippet:

var info = {"info":[{"ExamID":1,"ExamName":"Semester-1","MaxMarks":100},{"ExamID":4,"ExamName":"Test-1","MaxMarks":10},{"ExamID":5,"ExamName":"Test-2","MaxMarks":10}]}

var examArray = [];
for(var j = 0; j < info.info.length; j++){
  examArray.push(info.info[j]['ExamName']);
}

After running this code, examArray will contain "Semester-1", "Test-1", and "Test-2".

Answer №2

The store does not store data in an array, but rather in a Mixed Collection. You can retrieve the data using the following code:

var examNames = [];
for(var index = 0; index < length; index++){
   examNames.push(data.items[index].data['ExamName']);
}

Alternatively, you can consider obtaining the information directly from the source as suggested by others.

Another option to consider is using Store's collect method. Here's an example:

gridStore.collect('ExamName')

Answer №3

The issue arose from the asynchronous behavior of the store loading process:

var x = Store.load({
                    params : {CategoryID: CategoryData },
                   callback : function(results, operation, success){
                    var Snippet = []; // initialize empty array
                    Ext.each(results, function(entry) {
                        // specify the fields to be included
                        var Object = {
                            fourth_field: entry.get('ItemName')
                        };
                        Snippet.push(Object); // add object to the array
                    }, this);

                    console.log(Snippet);
                   }
});

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

Having Trouble Redirecting to Another Page from My Ionic Framework Controller?

Struggling to figure out why my app isn't switching to another page when clicking in my controller. Any help or pointers on what I might be missing or doing wrong would be greatly appreciated! Here's the code snippet from my app.js file: $s ...

Reading JSON files from the www directory of an Android Phonegap application with AngularJS

I'm attempting to access a local JSON file within the www directory of my PhoneGap application. Unfortunately, I tried using the $resource service in Angular to load the file, but it was unsuccessful. $resource('/app/www/app/Commun/JSonFiles/: ...

How can I incorporate fetch into my code using VSCode?

I am completely new to using JS. I want to incorporate fetch in VSCode, but I'm having trouble importing it. When I try: import fetch from "node-fetch"; I get this error: (node:19856) Warning: To load an ES module, set "type": &q ...

HTML5 input placeholder adapts its size and position dynamically as data is being

During my interaction with the input fields on my bank's website, I noticed that the placeholder text undergoes a unique transformation. It shrinks in size and moves to the upper-left corner of the input field while I am entering information. Unlike t ...

Asynchronous waterfall call in Node.js to call the method before

Is it possible to invoke a previous method within async.waterfall from a subsequent method? async.waterfall([ function (callback) { }, function (reservationStatus, callback) { }, function (reservationStatusList, f ...

Unable to extract data from JSON using PHP

JSON data: { "stocks": [ { "symbol": "AVGO", "name": "Broadcom Ltd.", "sector": "Technology", "industry": "Semiconductors", "SCTR": "97.8", "de ...

Experiencing difficulty importing Materialize CSS JS into React

Good day everyone, I've been facing challenges in implementing materialize css into my react-app, specifically with the JavaScript files. After trying various methods, I believe that I have made some progress using the following approach: In my &ap ...

Guide on inserting a JSON object into a structured array or recarray in NumPy

I'm facing a challenge where I need to convert JSON dictionary into a numpy record or structured array. The issue arises due to the random column order of data in JSON, making it difficult to append new data rows. Additionally, using pandas is not an ...

Can the "value" of an HTML form field rendered by Django be altered?

Essentially, I have a form that includes a radio select widget for the field named Queue. The choices available for the Queue are sourced from a model, which is accessed and defined within the views.py file. To display this form on my template, I am using ...

Locate elements within an array where a specific attribute includes the specified search term

While working with Javascript, I encountered an issue where the function I wrote to retrieve objects from an array was not returning all the data that met the query criteria. In my dataset, which originally contained 1536 objects, there are several jokes ...

"Encountering issue with componentDidMount() not functioning as expected, despite debugger being

Hello everyone! This is my first time sharing something here, so I'm eager to hear your feedback on both how I've posted and the content itself. I've only been programming for 8 weeks now, so I'm sure there are plenty of mistakes in wha ...

Using a `foreach` loop in PHP to organize data into groups

I am facing a challenge with two JSON files. One file contains information about leagues, including league id and league name, while the other file has details about fixtures, including fixture id and league id. My goal is to loop through the fixtures, gro ...

Guide to setting up and launching a JavaScript/Vue GitHub repository on your local machine

I have a cloned app located here: cvss-v4-calculator that I want to run locally for debugging with VS Code or a similar tool. However, I'm encountering difficulties in setting it up. I've been attempting to run this as a web page using node.js, b ...

The declaration file for module 'react/jsx-runtime' could not be located

While using material-ui in a react project with a typescript template, everything is functioning well. However, I have encountered an issue where multiple lines of code are showing red lines as the code renders. The error message being displayed is: Coul ...

Develop an interactive feature using React.js that allows users to manipulate the position of objects within a div

Looking to create a simple div with left and right buttons on either side that can shift the content in the center. An example of what I envision is shown here: Upon clicking the right arrow, the content should transition to this layout: Is there a Reac ...

Is there a way to retrieve the id of every post on my page?

Is it possible to send multiple post ids in JavaScript? I have successfully sent the first post id, but now I need to figure out how to send each individual post id. When inspecting the elements, I see something like this: <div data-id="post_1">< ...

Ways to verify if PHP associative arrays are the same, without considering the order of the keys?

Consider having two complex nested arrays in PHP, like the ones below: $c = array( "u" => array(3, 2, 1), "v" => array("w" => "hello", "x" => "world") ); $d = array( "v" => array("x" => "world", "w" => "hello"), " ...

When an AJAX call is made during a PHP session that has timed out

I am working on an AJAX form that handles data authentication. In the event of a session timeout, I need to implement a redirect to the login page. How can I go about achieving this? Here is an excerpt from my simplified server-side code: function doExecu ...

Tips for updating iframe source without refreshing the iframe?

I am facing an issue with two frames where I need to copy the content from the first frame (myframe) to the second frame (myframe2) using JavaScript. The challenge is that when I set the "src" attribute for myframe2, it causes a reload of FrameSecond. Is ...

When attempting to retrieve data saved to req.session with express-session from a different endpoint, the data appears to be

While working on my project with express-session, I encountered a peculiar issue. I am saving the currently logged in user to the session, but when I try to access the currentUser key on the get route, I find that the value is an empty object. Strangely, i ...