俗话说好,记性不如烂笔头。所以简单整理了一下自己工作中经常用的东西,这是自己第一次写博客,不喜勿喷!
对象转为字符串
const obj = {
id: 0,
name: '康康',
age: 26
}
const objToStr = JSON.stringify(obj)
console.log('obj:', obj)
console.log('objToStr:', objToStr)
json字符串转对象
const str = '{
"id":0,
"name":"康康",
"age":26
}'
const strToObj = JSON.parse(str)
console.log('str:', str)
console.log('strToObj:', strToObj)
以逗号分隔字符串转化为数组
const str = "wang,wei,kang";
const arr = str.split(',');
console.log('str:', str)
console.log('arr:', arr )
数组转换为以逗号分隔的字符串
const arr = ["wang","wei","kang"];
const str = arr.join(',');
console.log('arr:', arr )
console.log('str:', str)