js相同字符串截取拼接
原数据
const list = [
'999-1234567801',
'999-1234567802',
'999-1234567803',
];
const list1 = [
'999-1234567899',
'999-1234567900',
'999-1234567901',
];
期望数据
'999-1234567801/2/3' //list
'999-1234567899/900/901' //list1
处理代码
// 连续号码处理
export const formatNumber = (ticketNo: string[]): string => {
let point = 0;
let isTheSame = true;
const baseStr = ticketNo[0] || '';
for (let i = 0; i < baseStr.length; i++) {
const str = baseStr[point];
isTheSame = !ticketNo.some((t) => t[point] !== str);
if (isTheSame) {
point++;
} else {
break;
}
}
if (point === baseStr.length) {
return baseStr;
} else {
let msg = '';
ticketNo.map((t, index) => {
if (index === 0) {
msg = t;
} else {
msg = msg + `/${t?.substring(point, t?.length)}`;
}
});
return msg;
}
};