I am fairly new to typescript, and I am trying to simply remove an object from an array that I fetch from a mysql database, here is how I try to do it,
// this is the format of the fetched data
let permissions = [
{
id: '1',
permission: 'permission one'
},
{
id: '2',
permission: 'permission two'
},
{
id: '3',
permission: 'permission three'
},
{
id: '4',
permission: 'permission four'
},
]
permissions = permissions.filter(permission => {
return parseInt(permission.id, 10) !== 2 // 2 is just an example
});
when I try this way, I get the following compile error
Argument of type 'number' is not assignable to parameter of type 'string'
I also tried the following, but strangely enough, it always removes the last index!
const removeIndex = permissions
.map(item => item.id)
.indexOf(2); // also 2 is an example
permissions.splice(removeIndex, 1);
could someone please tell me where the error is? and also which one of the two methods is more performant? thanks in advance
2is not in the array ("2"is), thereforeindexOfreturns-1andsplice(-1, 1)removes the last element. Your first snippet should work, maybe someone broke the compiler again.