0

I get response from an api regarding user roles, if user has one role, I get single value place holder i.e

    {
  "nameid": "1",
  "unique_name": "Anees",
  "role": "Student",
  "nbf": 1587681052,
  "exp": 1587767452,
  "iat": 1587681052
}

If user has more than one roles, roles consider as array ie.

    {
  "nameid": "2",
  "unique_name": "Naveed",
  "role": [
    "SuperAdmin",
    "Admin",
    "Teacher",
    "Clerk",
    "Student"
  ],
  "nbf": 1587712850,
  "exp": 1587799250,
  "iat": 1587712850
}

How can I handle both a single value and a collection in same place holder?

This script work fit for me

const userRoles = this.decodedToken.role as Array<string>;

I have to use some collection methods like find etc

var status = userRoles.find(x => x == role);

it gives error in case of single value.

Any solution, please.

2
  • 1
    Honestly I'd ask the api owner if he could always return an array, but you can use Array.isArray to check for it. Commented Apr 24, 2020 at 7:45
  • working, thank you and api owner will be informed about your suggestion. Commented Apr 24, 2020 at 7:54

3 Answers 3

2

You could convert the single item to an array containing single element. Check if the element is an array using Array.isArray() function. Try the following

const userRoles = Array.isArray(this.decodedToken.role) ? this.decodedToken.role : [this.decodedToken.role];

let status = userRoles.find(x => x == role);

As a sidenote, using let instead of var helps to keep things the Typescript way.

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

2 Comments

It is actually better to generally declare variables as const, unless your code requires assigning to that variable later. (Which Typescript will complain about)
Yes I assumed OP intends to re-assign the variable down the line given that he/she has used const for userRoles, but var for status.
2

Casting will always succeed, so you need to check the type at runtime.

You can check to see if the value is an array using isArray

const isRolesArray = Array.isArray(userRoles)

var status = isRolesArray ? userRoles.find(x => x == role) : userRoles === role;

Or you can cast to the proper typescript type using union types and do a check using type guards. So you can do something like this

const userRoles = this.decodedToken.role as Array<string> | string;

let status

if (typeof userRoles === "string") {
 status = userRoles === role
} else {
 status = userRoles.find(x => x == role);
}

This way you get all the typechecking goodness from Typescript.

Comments

0

you can use something like this. Array.isArray() can easily check this

//declares a variable for object

let newobject = {
 "nameid": "2",
 "unique_name": "Naveed",
 "role": [
 "SuperAdmin",
 "Admin",
 "Teacher",
 "Clerk",
 "Student"
 ],
"nbf": 1587712850,
"exp": 1587799250,
"iat": 1587712850
}
function t(arr){
 let status;
 if(Array.isArray(arr)){
  status = arr.find(x => x == "Admin");
 } else {
  status = newobject.role;
 }
console.log('status ',status);
}
t(newobject.role); //call this for activating the check

Comments

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.