目录
一>字符串转数组
<1>字符串转一维数组:
1) 第一种方法: split( )
const n = 'abbdl' const b = n.split('') console.log('结果为:', b);//[ 'a', 'b', 'b', 'd', 'l' ]
2) 第二种方法: Array.from( )
let str = 'hello' let arr = Array.from(str) console.log(arr);//[ 'h', 'e', 'l', 'l', 'o' ]
3) 第三种方法: 扩展运算符(...)
let str = 'Hello' let arr = [...str] console.log(arr);// [ 'H', 'e', 'l', 'l', 'o' ]
4) 第四种方法: 字符串的match( )
let str = 'hello world' let arr = str.match(/\S+/g) console.log(arr);// [ 'hello', 'world' ]
5) 第五种方法: Array.prototype.map.call()
//Array.prototype.map(): 将字符串中每个字符转换成数字,并将它们存储在一个数组中 let str = '122344' let arr = Array.prototype.map.call(str,(x)=>{ return parseInt(x) }) console.log(arr);//[ 1, 2, 2, 3, 4, 4 ]
<2>字符串转二维数组:
1) 第一种方法: 使用split()和map()
let str = '1,2,3;e,r,t;喀' let arr = str.split(';').map(item=>item.split(',')) console.log(arr);//[ [ '1', '2', '3' ], [ 'e', 'r', 't' ], [ '喀' ] ]
<3>多个字符串转维数组:
const names = '张三' const age = 23 const color = '橘黑色' const infoArr = [names, age, color] //[ '张三', 23, '橘黑色' ]
二>数组转字符串
1) 第一种方法: 拼接字符串
const arr = ['one','two','three','four'] const str = arr + '' console.log(str);//one,two,three,four
2)第二种方法: 使用Array.prototype.toString()
const arr = ['one','two','three','four'] const str = arr.toString() console.log(str);//one,two,three,four
3)第三种方法:使用 Array.prototype.join方法
const arr = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday'] const str = arr.join(", "); console.log(str) // Sunday, Monday, Tuesday, Wednesday, Thursday
4) 第四种方法: 使用Array.prototype.toLocaleString方法
const arr = ['one','two','three','four'] const str = arr.toLocaleString() console.log(str);//one,two,three,four
5)第五种方法: 使用Array.prototype.join()
const arr = ['one','two','three','four'] const str = arr.join(',') console.log(str);// one,two,three,four
三>数组转对象的方法
1) 第一种方法: 使用ES6的扩展语法
const arr = ['one','two','three','four'] const obj = {...arr} console.log(obj);//{ '0': 'one', '1': 'two', '2': 'three', '3': 'four' }
2)第二种方法: 使用for循环
const arr = ['one','two','three','four'] const obj = {} for(let i=0;i<arr.length;i++){ obj[i]= arr[i] } console.log(obj);// { '0': 'one', '1': 'two', '2': 'three', '3': 'four' }
3) 第三种方法: 使用object.assign()方法
const arr = ['one','two','three','four'] const obj = Object.assign({},arr) console.log(obj);//{ '0': 'one', '1': 'two', '2': 'three', '3': 'four' }
4)第四种方法: 使用reduce()方法
/* array.reduce(function(total, currentValue, currentIndex, arr), initialValue) *prev:上次调用函数的返回值(必需) *currentValue:当前的元素(必需) *currentIndex:当前元素索引(可选) *arr:当前元素所属的数组对象(可选) *initialValue:传递给函数的初始值(可选) */ const arr = ['one', 'two', 'three', 'four'] const obj = arr.reduce((pre, cur, index) => { pre[index] = cur return pre }, {}) console.log(obj);//{ '0': 'one', '1': 'two', '2': 'three', '3': 'four' }
四>对象转数组的方法
1) 第一种方法:使用for...in
const obj = { a: 1, b: 2, c: 3 } const arr = [] for (let key in obj) { arr.push(obj[key]) } console.log(arr);// [ 1, 2, 3 ]
2)第二种方法:使用 Object.keys()
/* Object.keys():返回一个由对象的属性名组成的数组 */ const obj = { a: 1, b: 2, c: 3 } const arr = Object.keys(obj) console.log(arr);//[ 'a', 'b', 'c' ]
3) 第三种方法:使用Object.values()
/* Object.values():返回一个由对象的属性值组成的数组 */ const obj = { a: 1, b: 2, c: 3 } const arr = Object.values(obj) console.log(arr);//[ 1, 2, 3 ]
4) 第四种方法:使用Object.entries()
/* Object.entries():将对象的属性名和属性值转为二维数组。 */ const obj = { a: 1, b: 2, c: 3 } const arr = Object.entries(obj) console.log(arr);//[ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
5) 第五种方法:使用Array.from(obj)
/* Array.from():将类数组对象或可迭代对象转为数组 */ const obj1 = { 0: 'a', 1: 'b', 2: 'c', length: 3 }; const arr2 = Array.from(obj1); console.log(arr2); // ['a', 'b', 'c']
五>字符串转对象的方法
1)第一种方法:JSON.parse()
const str = '{"name":"小米","age":21}' const obj = JSON.parse(str) console.log(obj);//{ name: '小米', age: 21 }
2) 第二种方法: 字符串的eval()方法
onst str = '{"name":"小米","age":21}' const obj = eval("("+str+")") console.log(obj);//{ name: '小米', age: 21 }
3) 第三种方法:使用构造函数
const str = '{"name":"小米","age":21}' const obj = (new Function("return" +str))() console.log(obj);//{ name: '小米', age: 21 }