[英]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.