如何过滤数组仅选择React中对象同一ID的最后一个更改的值
这是我在Avfield的Onchange上运行的功能
let selectedFieldArray = []
const templateValue = (e) => {
const fieldObject = {
"id": e.target.name,
"value": e.target.value,
}
selectedFieldArray.push(fieldObject)
const uniqueID = selectedFieldArray.filter((obj, id, array) => {
return id === array.findIndex((t) => (
t.id === obj.id
))
})
}
,但是在solutionId中它仅选择数组的第一个对象,而不是数组中的最后更新对象。
This is the function i run on OnChange in AvField
let selectedFieldArray = []
const templateValue = (e) => {
const fieldObject = {
"id": e.target.name,
"value": e.target.value,
}
selectedFieldArray.push(fieldObject)
const uniqueID = selectedFieldArray.filter((obj, id, array) => {
return id === array.findIndex((t) => (
t.id === obj.id
))
})
}
but in uniqueID it only picks first object of array not the last updated object in array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是使用
arr.findindex
您可以使用arr.findlastindex
Instead of using
arr.findIndex
you can usearr.findLastIndex