使用函数和循环拼接字符串值并存储为数组 [英] Splicing string values using functions and loops and storing as an array

查看:159
本文介绍了使用函数和循环拼接字符串值并存储为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字列表,它是一个使用循环的字符串值,我想将此字符串拆分为数组中不同的变量,长度3的第一个,长度7的6和长度3的最后一个.这可以通过函数和循环来完成.

I have a list of numbers that is a string value using a loop I want to split this string into different variables in an array, the first of length 3 and the 6 of length 7 and the last of length 3. How can this be done using functions and loops.

推荐答案

您可以映射子字符串.

var str = '000111111122222223333333444444455555556666666mmmm',
    lengths = [3, 7, 7, 7, 7, 7, 7, 3],
    result = lengths.map((i => l => str.slice(i, i += l))(0));

console.log(result);

这篇关于使用函数和循环拼接字符串值并存储为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆