想要从数组中拼接元素

时间:2019-11-05 12:18:17

标签: javascript reactjs

我想从数组中拼接一个项目,我的数组就像

array=[{'PatientID':545, 'Name':jey, 'ref': d},
       {'PatientID':544, 'Name':tay, 'ref': dj},
       .... ] 

我写了一个用于拼接的代码,但是没有用, Uncaught TypeError: item.PatientID.indexOf is not a function 发生此错误

remove(id){
    var index = array.map(item=>{(item.PatientID).indexOf(id)})
....
}

如何解决我的问题?

1 个答案:

答案 0 :(得分:2)

根据index来找到id

const findIndex = id => array.findIndex(i => i.id === id)

const index = findIndex('545')

要根据ID删除项目

const deleteItem = id =>{
    return array.filter(i => i.id !== id)
}

const newArray = deleteItem(545)

更改数组

const deleteItem = id =>{
    const index = array.indexOf(i => i.id === id)
    array.splice(index, 1)
}