[Solved] Remove Index [number] from string [closed]


You can use map and replace

\[\d+\]$
  • \[ – Matches [
  • \d+ – Matches one or more digits
  • \] – Matches ]
  • $ – End of string

var errors = [
  { name: "Emails[2]", message: "Email is invalid" },
  { name: "Role", message: "Role is required" },
  { name: "Role random [123]", message: "Role is required" }
];


let op = errors.map(({name,...rest})=>{
  name = name.replace(/\[\d+\]$/,'').trim()
  return {
    name,
    ...rest
  }
})

console.log(op)

solved Remove Index [number] from string [closed]