1. 数组对象去重
filterList ( ) {
var arr = [ ] ,
cylinderNumber = [ ] ,
externalOrderNumber = [ ] ,
internalOrderNumber = [ ] ,
customer = [ ] ;
if ( _self. orderList. length == 0 ) {
_self. isShowSearchTip = true ;
return arr;
}
_self. orderList. forEach ( item => arr. push ( item) ) ;
if ( _self. filterText) {
cylinderNumber = _self. orderList. filter ( item => item. cylinderNumber. includes ( _self. filterText) ) ;
externalOrderNumber = _self. orderList. filter ( item => item. externalOrderNumber. includes ( _self. filterText) ) ;
internalOrderNumber = _self. orderList. filter ( item => item. internalOrderNumber. includes ( _self. filterText) ) ;
customer = _self. orderList. filter ( item => item. customer. includes ( _self. filterText) ) ;
}
const res = new Map ( ) ;
arr = cylinderNumber. concat ( externalOrderNumber) . concat ( internalOrderNumber) . concat ( customer) ;
_self. isShowSearchTip = false ;
return Array. from ( new Set ( arr) ) ;
}
2. 实现数字每三位加逗号的方法
function NumConversion ( str) {
var newStr = "" ;
var count = 0 ;
if ( str. indexOf ( "." ) == - 1 ) {
for ( var i = str. length - 1 ; i >= 0 ; i-- ) {
if ( count % 3 == 0 && count != 0 ) {
newStr = str. charAt ( i) + "," + newStr;
} else {
newStr = str. charAt ( i) + newStr;
}
count++ ;
}
str = newStr ;
return str;
}
else {
for ( var i = str. indexOf ( "." ) - 1 ; i >= 0 ; i-- ) {
if ( count % 3 == 0 && count != 0 ) {
newStr = str. charAt ( i) + "," + newStr;
} else {
newStr = str. charAt ( i) + newStr;
}
count++ ;
}
str = newStr + ( str + "00" ) . substr ( ( str + "00" ) . indexOf ( "." ) , 3 ) ;
return str;
}
}
3. 删除数组中的某一个对象
function removeArray ( _arr, _obj) {
varlength = _arr. length;
for ( vari = 0 ; i < length; i++ ) {
if ( _arr[ i] == _obj) {
if ( i == 0 ) {
_arr. shift ( ) ;
}
elseif ( i == length - 1 ) {
_arr. pop ( ) ;
}
else {
_arr. splice ( i, 1 ) ;
}
}
}
}
4. 字符串转base64和base64转字符串
let str= 'Hello Js!' ;
let str64= btoa ( string)
console. log ( atob ( str64) ) ;
function b64En ( str) {
return btoa ( encodeURIComponent ( str) ) ;
}
function b64Dec ( str) {
return decodeURIComponent ( atob ( str) ) ;
}
let str64 = b64En ( '你好' ) ;
console. log ( b64Dec ( str64) ) ;
5. 5种找出数组中最大值的方法
1. es6
Math. max ( ... arr)
2. es5
Math. max. apply ( null , arr)
3. for
let max = arr[ 0 ] ;
for ( let i = 0 ; i < arr. length - 1 ; i++ ) {
max = max < arr[ i+ 1 ] ? arr[ i+ 1 ] : max
}
4. sort
arr. sort ( ( n1, n2) => {
return n1 - n2 < 0
} )
arr[ 0 ]
5. reduce
arr. reduce ( ( n1, n2) => {
return n1 > n2 ? n1 : n2}
)
6. 查找所有含有特定class的a标签
console. log ( '1' , $ ( "#category" ) . find ( 'a[class="active"]' ) )
console. log ( '2' , $ ( "#category" ) . find ( 'a.active' ) )
console. log ( '3' , $ ( "#category a[class='active']" ) )
console. log ( '4' , $ ( "#category a.active" ) )
7. SCRIPT1003: 缺少 ‘:’,IE浏览器会报错,无法运行成功。而谷歌浏览器可以。
主要原因还是IE 兼容性较差,目前IE11 支持ES5 写法,对ES6 支持缺失。
let a = {
data ( ) {
return {
...
}
}
}
处理OK :
let a= {
data: function ( ) {
return {
...
}
}
}
8. ajax post请求 将参数放到body请求
$. ajax ( {
url: link + 'video/Online' ,
type: 'POST' ,
contentType: "application/json;charset=utf-8" ,
data: JSON . stringify ( { 'key' : 'key' } ) ,
dataType: "json" ,
success: function ( response) {
console. log ( 'getSessionIdLists' , response)
if ( 1 == response. code) {
return response. data. sessionId;
}
}
} )
9. 对小数进行判断处理
var point_position = String ( num) . indexOf ( "." ) + 1 ;
var count = String ( num) . length - point_position;
if ( point_position > 0 ) {
alert ( "这个数字是小数,有" + count + "位小数" ) ;
alert ( String ( num) . substring ( 0 , point_position- 1 ) )
} else {
var num = ( num || 0 ) . toString ( ) , result = '' ;
while ( num. length > 3 ) {
result = ',' + num. slice ( - 3 ) + result;
num = num. slice ( 0 , num. length - 3 ) ;
}
if ( num) {
result = num + result;
console. log ( '0' , result)
}
}
10. 抓取传过来的url的数据
window. location. search