2017-10-16 70 views
0

我有一個空的JavaScript數組(矩陣),我創建了實現div的刷新。我創建了一個動態地將數據放入其中的函數。然後我創建了一個函數來更新數組(我有問題)。 在數組中填充的數據是我放入JSON文件中的數據屬性。
如何動態更新JavaScript數組

爲了更好地undertand,這裏是我放在JSON文件我的數據屬性:

var currentAge  = $(this).data("age"); 
var currentDate  = $(this).data("date"); 
var currentFullName = $(this).data("fullname"); 
var currentIDPerson = $(this).data("idPerson"); 
var currentGender = $(this).data("gender"); 

數組的創建:

var arrayData = []; 

下面是一個創建於發起和addind功能元素到陣列:

function initMatrix(p_currentIDPerson, p_currentGender, p_currentFullName, p_currentDate, p_currentAge) { 
    var isFound = false;  
    // search if the unique index match the ID of the HTML one 
    for (var i = 0; i < arrayData.length; i++) { 
    if(arrayData[i].idPerson== p_currentIDPerson) { 
     isFound = true; 
    } 
    } 

    // If it doesn't exist we add elements 
    if(isFound == false) { 
     var tempArray = [ 
     { 
      currentIDPerson: p_currentIDPerson, 
      currentGender: p_currentGender, 
      currentFullName: p_currentFullName, 
      currentDate: p_currentDate, currentAge: p_currentAge 
     } 
     ]; 
     arrayData.push(tempArray); 
    } 
    } 

此處的更新函數是wh在我試過的時候,但它不起作用,也許我沒有用正確的方式編碼。如果你能幫忙請。

function updateMatrix(p_currentIDPerson, p_currentGender, p_currentFullName, p_currentDate, p_currentAge) { 
    for (var i = 0; i < arguments.length; i++) { 
    for (var key in arguments[i]) { 
     arrayData[i] = arguments[i][key];   
    } 
    } 
} 

要了解「$這一」和榆樹:榆木是我把點擊事件clickableDivs:

(function($) {  
    // Plugin to manage clickable divs 
    $.fn.infoClickable = function() { 
    this.each(function() { 
     var elm = $(this); 

     //Call init function 
     initMatrixRefresh(elm.attr("idPerson"), elm.data("gender"), elm.data("fullname"), elm.data("date"), elm.data("age")); 

     //call function update 
     updateMatrix("idTest", "Alarme", "none", "10-02-17 08:20", 10); 

     // Définition de l'evenement click 
     elm.on("click", function(){}); 
    }); 
    } 
    $('.clickableDiv').infoClickable(); 
}(jQuery)); 

預先感謝您

回答

1

嗯......我會建議你使用一個對象,其中每個關鍵是保持這個列表中的人的ID,而不是數組。通過這種方式,您可以編寫更簡潔的代碼,獲得相同的結果,但性能得到改進。例如:

var myDataCollection = {}; 

function initMatrix(p_currentIDPerson, p_currentGender, p_currentFullName, p_currentDate, p_currentAge) { 
    if (!myDataCollection[p_currentIDPerson]) { 
    myDataCollection[p_currentIDPerson] = { 
     currentIDPerson: p_currentIDPerson, 
     currentGender: p_currentGender, 
     currentFullName: p_currentFullName, 
     currentDate: p_currentDate, 
     currentAge: p_currentAge 
    }; 
    } 
} 

function updateMatrix(p_currentIDPerson, p_currentGender, p_currentFullName, p_currentDate, p_currentAge) { 
    if (myDataCollection[p_currentIDPerson]) { 
    myDataCollection[p_currentIDPerson] = { 
     currentGender: p_currentGender, 
     currentFullName: p_currentFullName, 
     currentDate: p_currentDate, 
     currentAge: p_currentAge 
    }; 
    } 
} 

根據您的業務邏輯,你可以刪除if語句,只保留一個功能,增加了該對象時沒有與指定的id的對象,當有一個更新的對象。

+0

感謝您的幫助,但我需要使用數組而不是列表,它會是相同的邏輯/代碼嗎? @Matheus – Zee

+0

嘿,@Zee!爲什麼數組是強制性的?你是否使用了一些需要它作爲數組的插件或API?否則,我不明白爲什麼不使用一個對象... –

+0

經過反思後,我確實像你說的那樣..更好......謝謝你的幫助(: – Zee

0

我覺得產生的基質的形狀與你想象的不同。具體來說,初始化後的矩陣看起來像[ [ {id, ...} ] ]。您的更新功能不夠循環。看起來你正在嘗試創建一個數據結構來存儲和更新用戶列表。我會推薦一個平面列表或由userID索引的對象,因爲這是您的查找。

var userStorage = {} 
 

 
// add/update users 
 
userStorage[id] = {id:u_id}; 
 

 
// list of users 
 
var users = Object.keys(users);