1

I wish to have an attribute called idField in my json object. Now this attribute is either an array if it contains multiple elements or just an object if it contains one element. I wish to create such an attribute from the data in an extjs store. In essence if the store contains just one element I need to insert this idField 'object' in the json object otherwise I need to insert an 'array' called idField in the json object. Please advise as to how to do that.

EDIT:

My store has 2 columns namely 'name' and 'type' in its data. What I wish is that if it contains just one element then my object should look like this:

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

If my store contains 2 rows then my object should look like this :

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

Also I have to INSERT this idField attribute inside the object. There is no currect idField attribute yet in the object.

EDIT 2:

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

1 Answer 1

2

To get the data from the EXT JS store

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

In my snippet below I am assuming that you like to get the values from field name called item.

var data = Ext.getStore("Your_Store_Name").data 
//imagine your JSON where you need to store is in variable myJSON 
if (data.items.length == 1){ 
myJSON.idField = {type: 'sometype', name: data.item[0]} } 
else if (data.item.length > 1)
{ //make an array of JSON 
var list = []; 
for(var i = 0; i < data.item.length; i++)
{  
list.push({type: 'sometype', name: data.item[i]})
} 
myJSON.idField = list; //set the array
}
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, thanks for the help. I have updated the post. Your approach won't work for my case since I don't have any idField attribute inside the object yet. I need to insert either an object or an array according to the number of elements in the store
OK, can you go to your browser console this Ext.getStore("Your_Store_Name").data can you paste a snippet of that.
I have posted a screenshot of the object I received in the console
var data = Ext.getStore("Your_Store_Name").data //imagine your JSON where you need to store is in variable myJSON if (data.items.length == 1){ myJSON.idField = {type: 'sometype', name: data.item[0]} } else if (data.item.length > 1){ //make an array of JSON var list = []; for(var i = 0; i < data.item.length; i++){ list.push({type: 'sometype', name: data.item[i]}) } myJSON.idField = list; }
Can you please update your answer itself because its a bit unclear in the comments. This way if it works I can chose it as the official answer as well for others to see.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.