Javascript——处理字符串单引号

比如:
let row.file= 733877064390737700,600879614404113000,65206129836717030
数据处理为:
,'733877064390737700','600879614404113000','65206129836717030'

方法封装:
// 单引号处理
      function addStrings(ids) {
        let arr = ids.split(',');
        let newArr = [];
        // 去除数组里面为空的
        arr.forEach(item => {
          if (item) {
            newArr.push(item);
          }
        });
        // 处理数据
        let op = '';
        for (let i = 0; i < newArr.length; i++) {
          if (i == newArr.length - 1) {
            op += `'${newArr[i]}'`;
          } else {
            op += `'${newArr[i]}',`;
          }
        }
        if (op) {
          return ',' + op;
        } else {
          return op;
        }
      }
 let row.files2=this.addStrings(row.file)
 console.log(row.files2)
 //,'733877064390737700','600879614404113000','65206129836717030'

你可能感兴趣的:(javascript)