Search code examples
javascriptobjectextjskeystore

Convert Store Data to Object with Defined Key Name ExtJS


based on this answer, I want to convert store data to object and defined the key value as well. Here's my related code :

var recordArray = {};
var paramArray = [];

store.each(function(record){
    recordArray.comment = record.get("comment");
    recordArray.datecreated = record.get("datecreated");
    paramArray.push(recordArray);
});

console.log(Ext.encode(paramArray));

But the printed out is only last data from store, with sum matches with data sum. Suppose I have 2 data from list view like this :

[{comment: a, datecreated:1-2-1999}, {comment: b, datecreated:2-2-1999}]

The print out :

[{comment: b, datecreated:2-2-1999}, {comment: b, datecreated:2-2-1999}] 

What I want, of course, the paramArray contains every object of listView, not just a same one. Any ideas? Help appreciated.


Solution

  • Try this,

    var paramArray = [];
    store.each(function(record){
        var recordArray = {};
        recordArray.comment = record.get("comment");
        recordArray.datecreated = record.get("datecreated");
        paramArray.push(recordArray);
    });
    

    In your code, you are overwriting the values in the original recordArray object instead of creating a new object everytime and since objects are passed by reference in JavaScript, the original recordArray reference at paramArray[0] also gets modified.