JSLite - Array object operations
If you have any questions, you are welcome to communicate in these places, and you are welcome to join the JSLite.io organization team for joint development!
Maximum (small) value
//顺带小教程 //获取最大值最小值 var a=[1,2,3,5]; console.log(Math.max.apply(null, a));//最大值 console.log(Math.min.apply(null, a));//最小值 var a=[1,2,3,[5,6],[1,4,8]]; var ta=a.join(",").split(",");//转化为一维数组 console.log(Math.max.apply(null,ta));//最大值 console.log(Math.min.apply(null,ta));//最小值
Array.remove
This is extended on the Array prototype object.
[1,5,6].remove(1)//⇒ [5, 6]
$.intersect
Array intersection
$.intersect([1,2,3,"asdkjf"],[2,3,6,"asdkjf"]) //⇒ [2, 3, "asdkjf"]
$.unique
Remove duplicates in the array element.
$.unique([1,2,12,3,2,1,2,1,1,1,1]) //⇒ [1, 2, 12, 3] var arr = $("#box").concat($("#box")) //两个一模一样的数组 $.unique(arr) //去重
$.sibling
Get the collection of node object attributes based on the type
(node,type)
.
$.sibling($("input"),"type") //⇒ ["text", "button", "checkbox"] $.sibling($("input"),"tagName") //⇒ ["INPUT"]
$.inArray
Search for the specified value in the array and return its index (returns
-1
if not found).
var arr = [ 4, "Pete", 8, "John" ]; $.inArray("John", arr); //⇒ 3 $.inArray(4, arr); //⇒ 0 $.inArray("David", arr); //⇒ -1 $.inArray("Pete", arr, 2); //⇒ -1
$.map
By traversing the node objects in the collection, a new array is returned through the function,
null
orundefined
will be filtered out.
$.map({"w":1,"c":2,"j":3},function(idx,item){ return item }); //⇒ ["w", "c", "j"]
$.each
General traversal method, can be used to traverse objects and arrays
$.each(["a", "b", "c"], function(index, item){ console.log("item %d is: %s", index, item) })
$.grep
Use the filter function to filter array elements.
$.grep( [0,1,2], function(n,i){ return n != 0; });
$.parseJSON
The same method as
JSON.parse
, accepts a standard format JSON string and returns the parsed JavaScript object.