1. 数组去重:
Array.from(new Set(arr_name))
let arr1 = ['张三','李四','王五','李四','赵六','张三'];let arr2 = Array.from(new Set(arr1));console.log(arr2)
输出结果:
['张三', '李四', '王五', '赵六']
2. 字符串分割成数组
str_name.split("分割条件","分割次数/默认全部")
let str1 = "张三,李四,王五,李四,赵六,张三";let arr1 = str1.split(",");console.log(arr1)console.log("-----")let arr2 = str1.split(",",2)console.log(arr2)
输出结果:
['张三', '李四', '王五', '李四', '赵六', '张三']-----['张三', '李四']
3. 数组拼接成字符串
arr_name.join("拼接参数")
不填参数,默认用
,
拼接, 用""
则无需拼接符直接拼接
let arr1=['张三','李四','王五','李四','赵六','张三'];let str1 = arr1.join()console.log(str1)console.log("-----")let str2 = arr1.join("")console.log(str2)
输出结果:
张三,李四,王五,李四,赵六,张三-----张三李四王五李四赵六张三
综合使用:
1.名单字符串去重
使用的时候,需要有分割规律(以,或其他分割的情况)
let str1 = "张三,李四,王五,李四,赵六,张三";let str2 = Array.from(new Set(str1.split(","))).join()console.log(str2)
输出结果:
张三,李四,王五,赵六