3

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

8
  • but this works fine in my chrome console Commented Apr 28, 2019 at 18:10
  • It works perfectly fine in an online typescript compiler such as typescriptlang.org/play Commented Apr 28, 2019 at 18:11
  • Wich Typescript version do you use? works in mine without problems Commented Apr 28, 2019 at 18:11
  • @JochenKühner, this code not even have TypeScript code, this is pure javaScript Commented Apr 28, 2019 at 18:13
  • 1
    The last snippet removes the last one because 2 is not in the array ("2" is), therefore indexOf returns -1 and splice(-1, 1) removes the last element. Your first snippet should work, maybe someone broke the compiler again. Commented Apr 28, 2019 at 18:16

1 Answer 1

4

Why don't you try this instead

permissions = permissions.filter(permission => +permission.id !== 2);

The Unary plus (+) operator is perfect here to evaluate a string as a number

Sign up to request clarification or add additional context in comments.

4 Comments

well it did work, could you please explain why or provide me with a link that does that!! thanks very much anyway :)
i too want to know, why it works, but in my case, i was running script in V8 engine and its was working fine
@MoSwilam & @DupinderSingh in my sample code the +identifier syntax is really saying "eval the value of identifier as a number" (c.f. MDN There are likely other ways this could be achieved: ts new Number(identifier); Number.parseInt(identifier); Number.parseInt(identifier, 10); The parseInt(identifier) and parseInt(identifier, 10) are also syntactically correct and should work.
@PopGoesTheWza, this is really helpful and useful, thanks mate, cheers!.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.