-1

I have Json Array data as shown below:

{
    "AccountNumber":[
        "123456789012",
        "234567891012",
        "345678912012"
    ]
}

Now I want to mask the middle 4 digit number only. as shown below:

{
    "AccountNumber":[
        "1234xxxx9012",
        "2345xxxx1012",
        "3456xxxx2012"
    ]
}

Can anyone help how to mask multiple json array data using javascript.

  • Are all numbers exactly 12 digit long? – Salman A Jul 29 '21 at 09:35
  • See [Why is “Can someone help me?” not an actual question?](//meta.stackoverflow.com/q/284236/4642212). Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and use the available static and instance methods of [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). Then, use the available [`String`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String#instance_methods) methods. – Sebastian Simon Jul 29 '21 at 09:40
  • @Salman A Yes. All numbers are 12 digits long. – Deval Soni Jul 29 '21 at 10:21
  • @SebastianSimon Thanks for your advice.. Next time I'll try to explain properly. – Deval Soni Jul 29 '21 at 10:23

1 Answers1

0

Since you say your working with JSON, the answer is as follows:

var json = '{"AccountNumber":["123456789012","234567891012","345678912012"]}';
var result = json.replace(/(\d{4})(\d{4})(\d+)/g, '$1xxxx$3');

// this will be the JSON with the masked values
console.log(result);
// now you can parse the JSON and get some useful result
console.log(JSON.parse(result));

However, it may be more useful to work with data parsed from JSON

var json = '{"AccountNumber":["123456789012","234567891012","345678912012"]}';
var data = JSON.parse(json);
data.AccountNumber.forEach((v, i, a) => a[i] = `${v.substr(0, 4)}xxxx${v.substr(8)}` );
console.log(data);
Bravo
  • 6,022
  • 1
  • 10
  • 15