JSON 与字符串互相转化

1
2
JSON.stringify(); // JSON转字符串
JSON.parse; // 字符串转JSON

字符串转数组

1
2
3
let m = "1,2,3,4,5,6";
let arr = m.split(",");
console.log(arr);

数组转字符串

1
2
3
let m = [1, 2, 3, 4, 5, 6];
let arr = m.join("-");
console.log(arr);

截取字符串

slice()

第一个参数代表开始位置,第二个参数代表结束位置的下一个位置,截取出来的字符串的长度为第二个参数与第一个参数之间的差;若参数值为负数,则将该值加上字符串长度后转为正值;若第一个参数等于大于第二个参数,则返回空字符串.

substring()

第一个参数代表开始位置,第二个参数代表结束位置的下一个位置;若参数值为负数,则将该值转为 0;两个参数中,取较小值作为开始位置,截取出来的字符串的长度为较大值与较小值之间的差.

substr()

第一个参数代表开始位置,第二个参数代表截取的长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var stmp = "www.baidu.com";
//使用一个参数
console.log(stmp.slice(3)); //从第4个字符开始,截取到最后个字符;返回".baidu.com"
console.log(stmp.substring(3)); //从第4个字符开始,截取到最后个字符;返回".baidu.com"
console.log(stmp.substr(3)); //从第4个字符开始,截取到最后个字符;返回".baidu.com"

//使用两个参数
console.log(stmp.slice(1, 5)); //从第2个字符开始,到第5个字符;返回"ww.b"
console.log(stmp.substring(1, 5)); //从第2个字符开始,到第5个字符;返回"ww.b"
console.log(stmp.substr(1, 5)); //从第2个字符开始,截取5个字符;返回"ww.ba"

//如果只用一个参数并且为0的话,那么返回整个参数
console.log(stmp.slice(0)); //返回整个字符串
console.log(stmp.substring(0)); //返回整个字符串
console.log(stmp.substr(0)); //返回整个字符串

//返回第一个字符
console.log(stmp.slice(0, 1)); //返回"w"
console.log(stmp.substring(0, 1)); //返回"w"
console.log(stmp.substr(0, 1)); //返回"w"

//在上面的例子中我们可以看出slice()和substring()的用法是相同的
//返回的值也是一样的,但当参数为负数时,他们的返回值却不一样,看下面的例子
console.log(stmp.slice(2, -5)); //返回"w.baid"
console.log(stmp.substring(2, -5)); //返回"ww"
console.log(stmp.substr(2, -5)); //返回空
//从上面两个例子可以看出slice(2,-5)实际上是slice(2,8)
//负5加上字符串长度13转换成正8(若第一位数字等于或大于第二位数字,则返回空字符串);
//而substring(2,-5)实际上是substring(2,0),负数转换为0,substring总是把较小的数作为起始位置。

数组去重

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function uniq(array) {
var temp = [];
var index = [];
var l = array.length;
for (var i = 0; i < l; i++) {
for (var j = i + 1; j < l; j++) {
if (array[i] === array[j]) {
i++;
j = i;
}
}
temp.push(array[i]);
index.push(i);
}
console.log(index);
return temp;
}

var aa = ["aaa", "bbb", "aaa", "ccc", "ddd", "aaa", "bbb", "ccc"];
console.log(uniq(aa)); // 输出去重结果

数组对象去重

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
let hash = {};
let config = [
{
name: 2,
state: true,
output: "Y",
},
{
name: 3,
state: true,
output: "A",
},
{
name: 5,
state: true,
output: "S",
},
{
name: 7,
state: true,
output: "B",
},
];

config = [
...config,
...config,
{
name: 3,
state: false,
output: "A",
},
];
console.log(config);
const newArr = config.reduceRight((item, next) => {
hash[next.name] ? "" : (hash[next.name] = true && item.push(next));
return item;
}, []);
console.log(newArr); // 输出去重结果

连接两个数组

1
concat();

转换大小写

1
2
3
4
5
toUpperCase(); // 转换大写
toLowerCase(); // 转换小写
// 针对特殊字符集
toLocaleUpperCase(); // 转换大写
toLocaleLowerCase(); // 转换小写

去除空格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let str = " 6 6 ";
console.log(str);
let str1 = str.replace(/\s*/g, ""); // 去除字符串内所有的空格
console.log(str1);
console.log(str);
let str2 = str.replace(/^\s*|\s*$/g, ""); // 去除字符串内两头的空格
console.log(str2);
console.log(str);
let str3 = str.replace(/^\s*/, ""); // 去除字符串内左侧的空格
console.log(str3);
console.log(str);
let str4 = str.replace(/(\s*$)/g, ""); // 去除字符串内右侧的空格
console.log(str4);
console.log(str);
// str.trim()方法
// trim()方法是用来删除字符串两端的空白字符并返回,trim方法并不影响原来的字符串本身,它返回的是一个新的字符串。
let str5 = str.trim();
console.log(str5);
console.log(str);