js姓名隐藏

/**
 * 只隐藏姓名中间的字
 * @param {data} 传入数据
 * 格式:张*三
 */
const hideCenterName = (name) => {
  let newStr;
  if (name.length === 2) {
    newStr = name.substr(0, 1) + '*';
  } else if (name.length > 2) {
    let char = '';
    for (let i = 0, len = name.length - 2; i < len; i++) {
      char += '*';
    }
    newStr = name.substr(0, 1) + char + name.substr(-1, 1);
  } else {
    newStr = name;
  }
  return newStr;
}
 
 
/**
 * 只显示姓氏
 * @param {data} 传入数据
 * 格式:张**
 */
const showFirstName = (name) => {
  let newStr;
  if (name.length === 2) {
    newStr = name.substr(0, 1) + '*';
  } else if (name.length > 2) {
    let char = '';
    for (let i = 0, len = name.length - 1; i < len; i++) {
      char += '*';
    }
    newStr = name.substr(0, 1) + char;
  } else {
    newStr = name;
  }
  return newStr;
}

 

posted @ 2022-05-19 14:53  abcByme  阅读(585)  评论(0编辑  收藏  举报