当前位置:网站首页>Intercept string fixed length to array
Intercept string fixed length to array
2022-07-03 03:43:00 【youhebuke225】
The first method ( recommend )
function sliceFixLenth(str,length) {
const result = [];
for (let i = 0; i <str.length ; i+=length) {
result.push(str.slice(i,i+length));
}
return result;
}
let str = "helloworld"
console.log(sliceFixLenth(str,1)); // ['h', 'e', 'l', 'l','o', 'w', 'o', 'r','l', 'd']
console.log(sliceFixLenth(str,2));// [ 'he', 'll', 'ow', 'or', 'ld' ]
console.log(sliceFixLenth(str,3));// [ 'hel', 'low', 'orl', 'd' ]
The second method
As an extension
function sliceFix(str,length) {
const count = Math.ceil(str.length / length);
let start = 0;
let end = 0;
const result = [];
for (let index = 0; index < count; index++) {
end += length;
result.push(str.slice(start,end))
start += length;
}
return result;
}
console.log("sliceFix",sliceFixLenth(str,3));
边栏推荐
- [learning notes] seckill - seckill project - (11) project summary
- redis在服务器linux下的启动的相关命令(安装和配置)
- [set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
- 【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
- Application of derivative in daily question
- [mathematical logic] predicate logic (individual word | individual domain | predicate | full name quantifier | existence quantifier | predicate formula | exercise)
- 【学习笔记】seckill-秒杀项目--(11)项目总结
- Recursive use and multi-dimensional array object to one-dimensional array object
- CEPH Shangwen network xUP Nange that releases the power of data
- C # webrequest post mode, based on "basic auth" password authentication mode, uploads files and submits other data using multipart / form data mode
猜你喜欢
Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
Ansible introduction [unfinished (semi-finished products)]
js中#号的作用
如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
PHP generates PDF tcpdf
Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop
Tidal characteristics of the Bohai Sea and the Yellow Sea
随机推荐
Change and access of median value of listening object
C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 03 operators and expressions
[DRM] simple analysis of DRM bridge driver call process
用Three.js做一個簡單的3D場景
Pytoch lightweight visualization tool wandb (local)
如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
Avec trois. JS fait une scène 3D simple
Elsevier latex 提交文章 pdftex.def Error: File `thumbnails/cas-email.jpeg‘ not found: using draf
Hutool dynamically adds scheduled tasks
Download and install node, NPM and yarn
Filter
leetcode:动态规划模板
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
Advanced redis applications [password protection, data persistence, master-slave synchronization, sentinel mode, transactions] [not completed yet (semi-finished products)]
Role of JS No
float与0比较
Use of sigaction
Mysql Mac版下载安装教程
Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
Recursive use and multi-dimensional array object to one-dimensional array object