javascript - 为什么 slice(0,-1) 不只删除数组上的一个数字
解决方案
您需要先检查字符串长度,然后根据该长度进行其余操作。
// check if last element length is more than 1
if(queue[queue.length - 1].length > 1){
// remove last character and update string
queue[queue.length - 1] = queue[queue.length - 1].slice(1, -1);
} else {
// else remove the last element
queue.pop();
}
或弹出最后一个元素,检查字符串的长度是否大于 1,然后删除最后一个字符并推送到数组。
// pop last element from the array
let last = quene.pop();
// check if element length is more than 1
if(last.length > 1){
// remove last character from string and push to the array again
queue.push(last.slice(1, -1))
}
推荐阅读
- ios - ios:内存文件操作符布尔崩溃
- javascript - 无法将属性“innerText”设置为 null
- python-3.x - 方 delay_capture 在 capture_transaction 上部分收费
- bash - 向多个文件中的现有哈希脚本添加更多哈希
- python - javascript 错误:arguments[0].scrollIntoView 不是在 python 上使用 selenium 的函数
- reactjs - 添加空的未使用的类名来反应组件
- kubernetes - Packstack 中的 Kubernetes 集群部署
- html - 焦点时表单输入图标消失
- jenkins - linux中关于jenkins的查询
- mysql - mysql覆盖单表权限