简体   繁体   English

如何在开始时转换数字,除了反应原生的最后4位数字

[英]How to convert number in start except last 4 digits in react native

In below response I am getting "43445567665" this value when I am mapping biomatricData.ninId:在下面的响应中,当我映射 biomatricData.ninId 时,我得到“43445567665”这个值:

biomatricData.ninId = 43445567665

Now I have to display only last 4 digit and rest should come like *现在我只需要显示最后 4 位数字,rest 应该像*

I have to change 43445567665 in below format我必须以以下格式更改43445567665

Like - *******7665喜欢 - *******7665

<View style={{ flexDirection: 'row', marginBottom: 10 }}>
  <RegularText text={'Nin Number :  '} textColor='grey' style={{ marginBottom: 5 }} />
  <Text>{biomatricData.ninId}</Text>
</View>

Put that biomatricData.ninId in variable then complete action given below and just use that variable for show.将该biomatricData.ninId放入变量中,然后完成下面给出的操作,并使用该变量进行显示。

use following regex.使用以下正则表达式。

 var str = "43445567665"; var replaced = str.replace(/.(?=.{4,}$)/g, '*'); console.log(replaced);

Try this code试试这个代码

var numToBeConverted = 327364829364;
String(numToBeConverted).split("").reverse().map((e, i) => i >= 4 ? "*" : e).reverse().join("");

You can make it a function if you want如果你愿意,你可以把它变成 function

function convertToBiometric(num) {
     return String(num).split("").reverse().map((e, i) => i >= 4 ? "*" : e).reverse().join("");
}

Can be achieved using Array methods like this.可以使用这样的Array方法来实现。

let numToBeConverted = '43445567665';

function covert(num){
  let arr = Object.values(num);
  return  arr.splice(0, arr.length-4).fill('*').join('')
  + arr.splice(-4).join('');
}

console.log(covert(numToBeConverted));
//output - "*******7665"

We can use String padding to add the * sign in our string values to convert our string in sensitive data我们可以使用字符串填充在我们的字符串值中添加 * 符号以将我们的字符串转换为敏感数据

checkout the doc to apply string padding签出文档以应用字符串填充

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM