1、switch多case单一操作
const currenty = $('#field18165span').text();
switch(currenty)
{
case 'USD':
$('#account').text('755911195032803');
break;
case 'CNY':
case 'HKD':
case 'EUR':
$('#account').text('755911195010801');
break;
default:
$('#account').text('');
}
2、数组操作
//1、unshift() 方法可向数组的“开头”添加一个或更多元素,并返回新的长度。
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple");
输出结果:Lemon,Pineapple,Banana,Orange,Apple,Mango
//2、push() 方法可向数组的“末尾”添加一个或多个元素,并返回新的长度。
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi")
输出结果:Banana,Orange,Apple,Mango,Kiwi
//2、splice() 方法用于添加或删除数组中的元素。
let arr = [1,2,3,4,5]
let newArr = arr.splice(0, 1)
// arr => [2,3,4,5]
// newArr => [1]
//userForm.value.authIds.splice(userForm.value.authIds.indexOf(parseInt(u)),1)
//3、将/image/1.jpg,/image/2.jgp转换为[{'url':'image/1.jpg'},{'url':'image/2.jpg'}]
let parts = productForm.value.picture.split(',');
let objects = parts.map(part => {
return { 'url': part }; // 返回一个对象,其中包含键和值
});
console.log('值为:' + JSON.stringify(objects))
fileList.value = objects
//4、循环遍历数组:[{'url':'image/1.jpg'},{'url':'image/2.jpg'}]
imagVal?.forEach((item) => {
productForm.value.picture += ',' + item.url
})
productForm.value.picture=productForm.value.picture.substring(1)
console.log('新值为:' + JSON.stringify(productForm.value.picture));
3、包含判断:includes() 方法用于判断字符串或数组是否包含指定的字符串或数组元素。
var str = "Hello world, welcome to the Runoob。";
var n = str.includes("Runoob");
userForm.value.authIds.splice(userForm.value.authIds.indexOf(parseInt(u)),1)
4、JS对象动态添加key-value存取
var map = {};
map['key1'] = 1;
map['key2@'] = 2;
console.log(map['key1']);//结果是1.
console.log(map['key2@']);//结果是2.
//如果遍历map
for(var prop in map){
if(map.hasOwnProperty(prop)){
console.log('key is ' + prop +' and value is' + map[prop]);
}
}
5、数组转换为字符串
// 一、toString() 方法
var a = [1,2,3,4,5,6,7,8,9,0]; //定义数组
var s = a.toString(); //把数组转换为字符串
console.log(s); //返回字符串“1,2,3,4,5,6,7,8,9,0”
console.log(typeof s); //返回字符串string,说明是字符串类型
var a = [1,2,3,4,5,6,7,8,9,0]; //定义数组
var b = [1,2,3,4,5,6,7,8,9,0]; //定义数组
var s = a + b; //数组连接操作
console.log(s); //返回“1,2,3,4,5,6,7,8,9,01,2,3,4,5,6,7,8,9,0”
var a = [1,[2,3],[4,5]],[6,[7,[8,9],0]]]; //定义多维数组
var s = a.toString(); //把数组转换为字符串
console.log(S); //返回字符串“1,2,3,4,5,6,7,8,9,0”
// join() 方法
var a = [1,2,3,4,5]; //定义数组
var s = a.join("=="); //指定分隔符
console.log(s); //返回字符串“1==2==3==4==5”
var a, b;
a = new Array(0,1,2,3,4);
b = a.join("-"); //"0-1-2-3-4"
b = a.join(); //"0,1,2,3,4"
6、字符串转换为数组
var s = "1==2== 3==4 ==5";
var a = s.split("==");
console.log(a);
console.log(a.constructor == Array);
const getLstInteger = (str:String)=>{
let lStrTemp = new Array();
str.split(',').map(u => {lStrTemp.push(parseInt(u))});
return lStrTemp;
}
7、四舍五入
方法一:Math.round保留2位小数就先乘于100再除于100,3位小数就1000
let tcsqje = Math.round(hsjeSum / sl * 100) / 100
方法二:toFixed()前面是整数得加().不然无法识别.是小数还是方法,返回值为字符串,转换Number直接乘于1或Number转换
let ce = (tcsqje - sqjeSum).toFixed(2) * 1;
let ce = Number((tcsqje - sqjeSum).toFixed(2));
8、JSON对象转化为字符串:JSON.stringify(dataArray[j])
9、避免Jquery ajax异步时多次请求出现重复数据的办法:改为同步请求 async: false
$.ajax({
url: 'http://192.168.3.15:800/user-serv/erp/getSdk',
async: false, //false代表同步
type: 'GET',
data:{
"site": fbbm,
"xmdcseq": xmdcseq,
"xmdc001": xmdc001
}