发起一次ajax异步请求,服务器返回data如下:
{
"data": [
{
"attr_id": 1,
"attr_name": "cpu",
"cat_id": 22,
"attr_sel": "only",
"attr_write": "manual",
"attr_vals": "人工,智能,大数据,前端,后端"
}
],
"meta": {
"msg": "获取成功",
"status": 200
}
}
attr_vals字符串各项以“ ,”隔开
使用split()方法
res.data.forEach((item) => {
item.attr_vals = item.attr_vals.split(',') //split方法中的分隔符为逗号
console.log(attr_vals)
// 得到数组attr_vals: ['人工','智能','大数据','前端','后端']
})
若字符串为"abc"
const text = "abc";
const chars = text.split(''); // 分隔符为空
// 得到数组chars:['a','b','c']
若字符串为"a b c"
const text = "a b c";
const chars = text.split(' '); // 分隔符为一个空格
// 得到数组chars:['a','b','c']