将数组添加到 extjs 中的 json 对象

Add an array to a json object in extjs

我希望在我的 json 对象中有一个名为 idField 的属性。现在这个属性要么是一个数组,如果它包含多个元素,或者只是一个对象,如果它包含一个元素。我希望从 extjs 存储中的数据创建这样一个属性。本质上,如果商店只包含一个元素,我需要在 json 对象中插入这个 idField 'object' ,否则我需要在 json 对象中插入一个名为 idField 的 'array' 。请告知如何做到这一点。

编辑:

我的商店在其数据中有 2 列,即 'name' 和 'type'。我希望的是,如果它只包含一个元素,那么我的对象应该如下所示:

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

如果我的商店包含 2 行,那么我的对象应该如下所示:

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

我还必须在对象中插入此 idField 属性。对象中还没有当前的 idField 属性。

编辑 2:

我在写console.log(Ext.getStore("My_store_name").data)

的时候在控制台收到了这个对象

从 EXT JS 存储中获取数据

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

在下面的代码片段中,我假设您希望从名为 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
}