简体   繁体   English

想要从数组中拼接一个元素

[英]Want to splice an element from an array

I want to splice an item from an array, my array is like,我想从一个数组中拼接一个项目,我的数组就像,

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

I wrote a code for splicing, but it does'nt work, Uncaught TypeError: item.PatientID.indexOf is not a function this error is occured我写了一段拼接代码,但是不行, Uncaught TypeError: item.PatientID.indexOf is not a function这个错误

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

How to resolve my issue?如何解决我的问题?

To find the index based on the id根据id查找index

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

const index = findIndex('545')

To remove an item based on the id根据 id 删除项目

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

const newArray = deleteItem(545)

Mutating the array改变数组

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM