判空
在node中,可以通过下面的方式判空:
if (a)
此时如果a为null或者undefined,则a的值相当于false,如果a存在值,非空,则a的值相当于true。
测试程序
let a;
console.log(a === null)
console.log(a === undefined)
if (a){
console.log("a不为空")
}
if (!a){
console.log("a为空")
}
let b = null
console.log(b === null)
console.log(b === undefined)
if (b){
console.log("b不为空")
}
if (!b){
console.log("b为空")
}
测试结果
false
true
a为空
true
false
b为空
String
字符串的替换,包括替换单个字符串中的某个字符串,替换多个字符串中的某个字符串。
replace
替换匹配的第一个字符串
let resourcesInf = "com.android.support:appcompat-v7:28.0.0".replace(".","_").replace(":","---").split("---");
console.log(resourcesInf)
输出:
[ 'com_android.support', 'appcompat-v7:28.0.0' ]
replaceAll
let resourcesInf = "com.android.support:appcompat-v7:28.0.0".replaceAll(".","_").replaceAll(":","---").split("---");
console.log(resourcesInf)
输出:
[ 'com_android_support', 'appcompat-v7', '28_0_0' ]
node低版本中replaceAll不适用
此时,如果进行批量替换时,需要使用正则的方式进行替换。
替换格式如下:
/待替换字符/g
如果代替换字符串为特殊字符,则需要使用转义字符\
let resourcesInf = "com.android.support:appcompat-v7:28.0.0".replace(/\./g, "_").replace(/\:/g, "---").split("---");
Map
遍历
var myMap = new Map();
myMap.set(0, "zero");
myMap.set(1, "one");
for (var key of myMap.keys()) {
console.log(key);
}
// Will show 2 logs; first with "0" and second with "1"
for (var value of myMap.values()) {
console.log(value);
}
// Will show 2 logs; first with "zero" and second with "one"
for (var item of myMap.entries()) {
console.log(item[0] + " = " + item[1]);
}
// Will show 2 logs; first with "0 = zero" and second with "1 = one"
myMap.forEach(function(value, key) {
console.log(key + " = " + value);
}, myMap)
// Will show 2 logs; first with "0 = zero" and second with "1 = one"
let publicResource = new Map();
let resourcesInf = "com.android.support:appcompat-v7:28.0.0".replaceAll(".","_").replaceAll(":","---").split("---");
console.log(resourcesInf)
publicResource.set(resourcesInf[0]+"---"+resourcesInf[1],resourcesInf[2])
console.log(publicResource)
publicResource.forEach(function (value, key){
console.log(key)
console.log(value)
})
console.log('11111')
输出:
[ 'com_android_support', 'appcompat-v7', '28_0_0' ]
Map(1) { 'com_android_support---appcompat-v7' => '28_0_0' }
com_android_support---appcompat-v7
28_0_0
11111
String Array
使用indexOf判断,如果字符串数组不包括字符串,则返回-1;包括返回index。
targetPermissionList.indexOf(resourceUsePermissionJson[i][attrPre + 'android:name']) === -1