JS常用小功能

2021-09-10 22:48:42 字數 1539 閱讀 7263

個人部落格:

常用小功能/

陣列去重

字串反轉

陣列去扁平化並去重排序

判斷陣列的方法

陣列並集、交集、差集

var arr1 =[1

,2,3

,4];

var arr2 =[2

,4,6

,8];

// 使用includes

var union = arr1.

concat

(arr2.

filter

(v =>

!arr1.

includes

(v)));

// 並集

console.

log(union)

;var intersection = arr1.

filter

(v=> arr2.

includes

(v))

;// 交集

console.

log(intersection)

;var difference = arr1.

concat

(arr2)

.filter

(v =>

!arr1.

includes

(v)||

!arr2.

includes

(v))

;// 差集

console.

log(difference)

;// 使用set

var seta =

newset

(arr1)

;var setb =

newset

(arr2)

;var union1 = array.

from

(new

set(arr1.

concat

(arr2)))

;var union2 = array.

from

(new

set(

[...arr1,

...arr2]))

;console.

log(union1)

;// [1, 2, 3, 4, 6, 8]

console.

log(union2)

;// [1, 2, 3, 4, 6, 8]

var intersection1 = arr1.

filter

(v=> setb.

has(v));

console.

log(intersection1)

;var difference1 = arr1.

concat

(arr2)

.filter

(v =>

!seta.

has(v)

||!setb.

has(v));

console.

log(difference1)

;

JS實用小功能

獲取使用者ip ua 城市 操作cookie function getcookie c name return unescape document.cookie.substring c start,c end return function setcookie c name,value,expire...

js 小功能收藏

1,固定表頭 固定表頭 function theadfixed id 2,給某個元素外層加標籤 id wrapall 3,json陣列按照某個字段排序 按照某個字段排序 function objsort prop if val1 val2 else if val1 val2 else 4,得到大寫字...

js小功能記錄

個人日常中遇到的js小功能記錄,方便檢視。判斷是否包含字串某字串 param str 被檢測的字串 param substr 檢測是否含有的字串 return ture,false function iscontains str,substr 判斷檔案是否是 param filetype 檔案型別,...