JSLite - 數(shù)組對象操作

2018-12-07 00:35 更新

如有疑問歡迎到這些地方交流,歡迎加入JSLite.io組織團伙共同開發(fā)!

segmentfault社區(qū) | 官方網(wǎng)站 | 官方文檔-更詳細 | Issues

最大(小)值

  1. //順帶小教程
  2. //獲取最大值最小值
  3. var a=[1,2,3,5];
  4. console.log(Math.max.apply(null, a));//最大值
  5. console.log(Math.min.apply(null, a));//最小值
  6. var a=[1,2,3,[5,6],[1,4,8]];
  7. var ta=a.join(",").split(",");//轉(zhuǎn)化為一維數(shù)組
  8. console.log(Math.max.apply(null,ta));//最大值
  9. console.log(Math.min.apply(null,ta));//最小值

Array.remove

這個是在Array原型對象上擴展的。

  1. [1,5,6].remove(1)//? [5, 6]

$.intersect

數(shù)組交集

  1. $.intersect([1,2,3,"asdkjf"],[2,3,6,"asdkjf"])
  2. //? [2, 3, "asdkjf"]

$.unique

刪除數(shù)組中重復元素。

  1. $.unique([1,2,12,3,2,1,2,1,1,1,1]) //? [1, 2, 12, 3]
  2. var arr = $("#box").concat($("#box")) //兩個一模一樣的數(shù)組
  3. $.unique(arr) //去重

$.sibling

根據(jù)類型獲取節(jié)點對象屬性的集合 (node,type)。

  1. $.sibling($("input"),"type") //? ["text", "button", "checkbox"]
  2. $.sibling($("input"),"tagName") //? ["INPUT"]

$.inArray

搜索數(shù)組中指定值并返回它的索引(如果沒有找到則返回-1)。

  1. var arr = [ 4, "Pete", 8, "John" ];
  2. $.inArray("John", arr); //? 3
  3. $.inArray(4, arr); //? 0
  4. $.inArray("David", arr); //? -1
  5. $.inArray("Pete", arr, 2); //? -1

$.map

通過遍歷集合中的節(jié)點對象,通過函數(shù)返回一個新的數(shù)組,nullundefined 將被過濾掉。

  1. $.map({"w":1,"c":2,"j":3},function(idx,item){
  2. return item
  3. });
  4. //? ["w", "c", "j"]

$.each

通用例遍方法,可用于例遍對象和數(shù)組

  1. $.each(["a", "b", "c"], function(index, item){
  2. console.log("item %d is: %s", index, item)
  3. })

$.grep

使用過濾函數(shù)過濾數(shù)組元素。

  1. $.grep( [0,1,2], function(n,i){
  2. return n != 0;
  3. });

$.parseJSON

JSON.parse 方法相同,接受一個標準格式的 JSON 字符串,并返回解析后的 JavaScript 對象。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號