Abhijeet Ahuja
Abhijeet Ahuja

Reputation: 5970

Replace with uuid(or random number)

I want to replace content in json with a unique number, say uuid. My json is

let response =   
{
  "data": {
    "address": [
      {
        "city": "wildcard"
      },
      {
        "city": "wildcard"
      }
    ]
  }
}

I want to replace wildcard with unique numbers.

My code is

response = response.replace(new RegExp('wildcard','g'), uuid.v4());

its replacing all the wildcard in the json with a single UUID, I want each wildcard to be replaced with a unique uuid.v4().

Basically I need help to call uuid.v4() each time while replacing the wildcard ?

Upvotes: 0

Views: 1330

Answers (1)

Abhijeet Ahuja
Abhijeet Ahuja

Reputation: 5970

response = response.replace(new RegExp('@uuid','g'),() => uuid.v4());

worked for me. Reason: Replacing that with a function which returns uuid makes sure a unique id is returned every time that function is called.

Upvotes: 1

Related Questions