详细举例JS字符操作,包括串替换、字符串转数组、数组过滤
1、字符串替换
//替换,字符串回车字符替换成逗号
let str1="aaa\nbbb\n\n\ccc\n\nddd";
let str2=children.replace(/\n/g, ",");
console.info(str2);
//结果aaa,bbb,,ccc,,ddd
2、字符串转数组
//字符串转数组
let str2="aaa,bbb,,ccc,,ddd",
let str2List = str2.split(",")
console.info(str2List);
//结果["aaa","bbb","","ccc","","ddd"]
3、数组过滤
let str2List=["aaa","bbb","","ccc","","ddd"]
//过滤空行数据
let str2List2 = str2List.filter((d) => {
return (d) && d != "";//判断条件可以按需调整
});
console.info(str2List);
//结果["aaa","bbb","ccc","ddd"]
去除重复的
let list1=[1,2,3,4,5,6];
let list2=[2,3,5,6,7,8,9];
const diff = list1.filter(x => !list2.includes(x));
console.info(diff);
//结果[1,4]