我这样去为什么去不掉'[',']'
let str1 = JSON.stringify(this.checkboxGroupFrom)//[1,2,3,4,6,7]
typeof(str1)
str1.replace("[","")
str1.replace("]","")
console.log(str1)
我这样去为什么去不掉'[',']'
let str1 = JSON.stringify(this.checkboxGroupFrom)//[1,2,3,4,6,7]
typeof(str1)
str1.replace("[","")
str1.replace("]","")
console.log(str1)
this.checkboxGroupFrom = [1,2,3,4,6,7]
str1 = this.checkboxGroupFrom.join(',') //1,2,3,4,5,6,7
str2 = this.checkboxGroupFrom.join('') //1234567
let str1 = JSON.stringify(this.checkboxGroupFrom)//[1,2,3,4,6,7]
typeof(str1)
str1 = str1.replace("[","")
str1 = str1.replace("]","")
console.log(str1)
// 缺少赋值
这是typescript吗?
let str1 = JSON.stringify(this.checkboxGroupFrom)//[1,2,3,4,6,7]
typeof(str1)
let st2=str1.replace("[","")
let st3=st2.replace("]","")
console.log(st3);
不过建议你用正则表达式 str1.replace(/[\[\]]/g,'');
let str1 = JSON.stringify(this.checkboxGroupFrom);
let str = /^\[(.*?)\]$/.exec(str1)[1];
console.log(str);
this.checkboxGroupFrom = [1,2,3,4,5,6,7];
let str1 = JSON.stringify(this.checkboxGroupFrom);//[1,2,3,4,6,7]
console.log(typeof(str1));
let str = str1.substr(1);
let strNew = str.substr(0,str.length-1);
console.log(strNew);
this.checkboxGroupFrom = [1,2,3,4,5,6,7];
let str1 = JSON.stringify(this.checkboxGroupFrom);//[1,2,3,4,6,7]
console.log(typeof(str1));
let str = str1.replace("[","");
let strNew = str.replace("]","");
console.log(strNew);
试试这两个
10 回答11.3k 阅读
5 回答4.9k 阅读✓ 已解决
4 回答3.2k 阅读✓ 已解决
2 回答2.8k 阅读✓ 已解决
3 回答2.4k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
上面这些哥们都没回答到重点,replace 不会改变原来的字符串,而是会产生一个新的字符串。
mdn 文档这样定义的:
参考链接:https://developer.mozilla.org...
况且你要拿到数组里面的每个分量根本不用这样做啊,直接用 join 方法:
参考链接:https://developer.mozilla.org...