2017-02-27 39 views
0

基於this答案,我想將存儲數據轉換爲對象並定義鍵值。這裏是我相關的代碼:將存儲數據轉換爲包含已定義鍵名的對象ExtJS

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)); 

但是從商店打印出來只有最後的數據,與和數據之和相匹配。假設我有一個像這樣從列表視圖數據:

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

打印出來:

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

我想要什麼,當然,使用ParamArray包含的ListView的每個對象,不只是同一個。有任何想法嗎?幫助讚賞。

回答

2

試試這個,

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

在代碼中,你在原來的recordArray對象覆蓋值,而不是創建一個新的對象,每次和因爲對象是通過引用在JavaScript中,在原來的recordArray引用傳遞paramArray [0]也被修改。

相關問題