数组的转换

数组转换成字符串

数组转换成字符串的方法很多,其中用到最多的两种:join,toSpring

  • join
  • join是拼接,是可以用符号来拼接,一般根据场景需要拼接
  •   let array = [1,2,3];
      let newArray = array.join('-');
      console.log(newArray);  // 1-2-3
    

``

  • toSpring
  •   let array = [1,2,3,4];
      let newArray = array.spring();
      console.log(newArray); // 1,2,3,4
    

``

字符串转换成数组

字符串转换成数组的时候情况不同也是不同的;

  • split
	//当字符串时相连得时候
	let testSpring = 'helloworld'let newTestSpring = testSpring.split(',');
	console.log(newTestSpring);//  ["helloworld"]
	let newTestSpring2 = testSpring.split('');
	console.log(newTestSpring2);//["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
	//当字符串之间有字符串连接的时候
	let testSpring2 = 'hello,world';
	let testSpring2Array = testSpring2.split(',');
	console.log(testSpring2Array); //["hello", "world"]
	let testSpring2Array2 = testSpring2.split('');
	console.log(testSpring2Array);//["hello", "world"]

split();其实大致和join的原理相差不多,()内容没有分割符的时候也是默认显示的是‘,’;然后是根据字符串中之间的符号来分裂

伪数组转换为新数组

Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map).

	let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
};
// ES5的写法
var arr1 = [].slice.call(arrayLike); // ['a', 'b', 'c']
// ES6的写法
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
// 在例如上面写的split的方法利用
let testSpring = 'helloworld'let newTestSpring = Array.from(testSpring);
console.log(newTestSpring);//["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]

原文章连接:点击
关于es6数组的文章总结(暂时不全):http://www.wangcanghai.cn/detailes?id=601b706524fc4a133c24648c
访问支持一下小博主嘿嘿

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值