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