-2

My array data is like this

    "userRoles": [
    {
      "id": 1,
      "name": "Create",
      "description": "This is For Create ",
      "checked": false,
      "isDeleted": false,
      "applicationId": 1,
      "application": "DMTS",
      "totalPermissions": 0
    },
    {
      "id": 2,
      "name": "Update",
      "description": "This is For Update",
      "checked": false,
      "isDeleted": false,
      "applicationId": 1,
      "application": "DMTS",
      "totalPermissions": 0
    }
]

I want the data ids in this format ["1,2"] in TypeScript, please help me in this case.

Nick Vu
  • 14,512
  • 4
  • 21
  • 31

3 Answers3

0
const idsArray = userRoles.map(role => `${role.id}`)
0
const t1 = { "userRoles": [
    {
      "id": 1,
      "name": "Create",
      "description": "This is For Create ",
      "checked": false,
      "isDeleted": false,
      "applicationId": 1,
      "application": "DMTS",
      "totalPermissions": 0
    },
    {
      "id": 2,
      "name": "Update",
      "description": "This is For Update",
      "checked": false,
      "isDeleted": false,
      "applicationId": 1,
      "application": "DMTS",
      "totalPermissions": 0
    }
]};

const t2 = t1.userRoles.map(m => m.id); // (2) [1, 2]
Artur Z
  • 1
  • 2
0
type Application = "DMTS" | "ETC"
interface UserRole {
  id: number,
  name: string,
  description: string
  checked: boolean
  isDeleted: boolean
  applicationId: number
  application: Application
  
}
 const userRoles: UserRole[] = [
    {
      id: 1,
      name: "Create",
      description: "This is For Create ",
      checked: false,
      isDeleted: false,
      applicationId: 1,
      application: "DMTS",
      totalPermissions: 0
    },
    {
      id: 2,
      name: "Update",
      description: "This is For Update",
      checked: false,
      isDeleted: false,
      applicationId: 1,
      application: "DMTS",
      totalPermissions: 0
    }
]
const ids: number[] = userRoles.map(role => role.id)
let result: String[]
let str: String = ""
ids.forEach((id, index) => {
  str.concat(id)
  if (index < ids.size) str.concat(',')
})
return result
TED911
  • 1
  • 2