簡體   English   中英

Lodash - 用相同的索引替換數組中的 object

[英]Lodash - replace object in array with the same index

我想用數組中的相同 ID 替換 object。 我已經使用了.unionBy 使用 Lodash。 問題是新的 object 出現在第一個而不是相同的索引處。

這是我的代碼。 希望您能夠幫助我。 謝謝!

state.allStudents是數組。 學生們是新的 object 來替換陣列中具有相同 _id 的現有 object

state.allStudents = _.unionBy([students], state.allStudents, '_id');

我會使用.findIndex.splice

    let index = state.allStudents.findIndex(i => i._id === students._id);
    if (index != -1) {
        state.allStudents.splice(index, 1, students);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM