1、数据情况:
其一、从后端拿到的数据为: let arr = [1,[10,20],[20,30],40]
其二、目标数据为:'1,10-20,20-30,40'
2、操作过程:
其一、将数据转化成: [1, '10-20', '20-30', 40]
A、代码展示:
let arr = [1,[10,20],[20,30],40]
let first_res = arr.map(item => {
if(Array.isArray(item)) {
// 此时是为了区分 item 是二维的数组还是非数组的值;
let res = item.join("-") // 此时是将二维数组中的 ',' 换成 '-' 的操作;
return res // 将操作后的二维数组返回;
} else {
return item // 将非数组的值返回;
}
})
console.log(first_res,565656); // 此时的输出结果为:[1, '10-20', '20-30', 40]
B、结果展示:
其二、将数据转化为目标数据:'1,10-20,20-30,40'
A、代码展示:
let first_res = [1, '10-20', '20-30', 40]
let second_res = first_res.map(it => {
if(typeof it === "string") {
// 此时是区分:it 是字符串类型和非字符串类型;
return it // 字符串类型就直接返回;
} else {
let res = it.toString() // 非字符串类型就转化成字符串类型再返回;
return res
}
})
console.log(second_res, 787878787); // 此时的输出结果为:['1', '10-20', '20-30', '40'] (即:字符串类型的数组)
console.log(second_res.toString(),78978999999); // 此时的输出结果就是目标值:'1,10-20,20-30,40'
B、结果展示:
3、小结:
其一、哪里有不对或不合适的地方,还请大佬们多多指点和交流!
其二、若有转发或引用本文章内容,请注明本博客地址(直接点击下面 url 跳转
) https://blog.csdn.net/weixin_43405300,创作不易,且行且珍惜!
其三、有兴趣的话,可以多多关注这个专栏(Vue(Vue2+Vue3)面试必备专栏)(直接点击下面 url 跳转
):https://blog.csdn.net/weixin_43405300/category_11525646.html?spm=1001.2014.3001.5482