<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>新建文件夹02 补位+排序</title>
</head>
<body>
<script>
let data = [
"我的音乐",
"我的收藏",
"新建文件夹",
"新建文件夹(3)",
];
document.onclick = function(){
let names = data.filter((item)=>{
if(item === "新建文件夹"){
return true;
};
if(item.substring(0,6) === "新建文件夹(" && Number(item.substring(6,item.length-1))>=2 && item[item.length-1]=== ")"){
return true;
};
return false;
});
names.sort((n1,n2)=>{
n1 = n1.substring(6,n1.length-1);
n2 = n2.substring(6,n2.length-1);
n1 = isNaN(n1)?0:n1;
n2 = isNaN(n2)?0:n2;
// console.log(n1,n2)
return n1 - n2;
});
if(names[0] !== "新建文件夹"){
data.push("新建文件夹");
return;
};
for(let i = 1;i<names.length ;i++){
if( Number(names[i].substring(6,names[i].length-1)) !== i+1){
// console.log(`截取的内容:${names[i].substring(6,names[i].length-1)}`,i+1);
// console.log(`缺失的内容:${i+1}`);
data.push(`新建文件夹(${i + 1})`);
return;
}
};
data.push(`新建文件夹(${names.length + 1})`);
console.log(data)
};
</script>
</body>
</html>
运行结果
网友评论