Home > Net >  How to replace square brackets and set dot before replacement?
How to replace square brackets and set dot before replacement?

Time:10-04

Good day.

I wish to replace square brackets inside string and set dot before replacement.

For example, Current string position[0].type i need transform to next one position.0.type

I have tried making it by this rule 'position[0].type'.replace(/[[\]]/g, '') as expected, will removed only brackets, how i can set needed dot?

Thanks.

CodePudding user response:

You can use

.replace(/[[\].] /g, '.')

The [[\].] regex matches one or more ( ) [, ] or . chars. g makes it match all occurrences and '.' replaces the matches with a single ..

CodePudding user response:

Firstly property name should be string , then let you try to make it number , you cant call it directly like obj.0 or obj.2 , you can call like obj['1'] or obj.[1] and if u dont want to give up finally u can do

let aObj = {
    0:{
        type:'exampleType'
    }    
}
const { 0:numkey } = aObj; 

console.log(numkey.type) 
  • Related