在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Array.js开源软件地址:https://gitee.com/liuxiangchao/Array.js开源软件介绍:Array.jsArray.js帮助开发人员更优雅地操作Javascript数组。 使用直接引入 Array.js 或 Array.min.js: <script type="text/javascript" src="Array.min.js?v=0.0.3"></script> 然后在Javascript中使用: var arr = [ 1, 2, 3, 4];arr.$each(function (k, v) { console.log(v);}); 其中 API增
删
改
查
辅助
迭代器在参数中使用 var has = [1, 2, 3, 4].$any(function (k, v) { return v > 10;}); 排序迭代器在 var arr = [3, 2, 4, 1];arr.$sort(function (v1, v2) { if (v1 > v2) { return 1 } if (v1 == v2) { return 0; } return -1;});//现在 arr = [1, 2, 3, 4] 文档$pad
示例代码1: var arr = [1, 2, 3];arr.$pad("a"); // arr => [1, 2, 3, "a"] 示例代码2: var arr = [1, 2, 3];arr.$pad("a", 5); // arr => [1, 2, 3, "a", "a", "a", "a", "a"] $fill
示例代码1: var arr = [1, 2, 3];arr.$fill("a", 5); // arr => [1, 2, 3, "a", "a"] $push
示例代码1: var arr = [1, 2, 3];arr.$push(4); // arr => [1, 2, 3, 4]arr.$push(5, 6, 7, 8); // arr => [1, 2, 3, 4, 5, 6, 7, 8] $pushAll
示例代码1: var arr = [1, 2, 3];arr.$pushAll([4, 5, 6]); // arr => [1, 2, 3, 4, 5, 6] $insert
示例代码1: var arr = [1, 2, 3];arr.$insert(0, "a", "b", "c"); // arr => ["a", "b", "c", 1, 2, 3] 示例代码2: var arr = [1, 2, 3, 4, 5];arr.$insert(2, "a", "b", "c"); // arr => [1, 2, "a", "b", "c", 3, 4, 5] 示例代码3: var arr = [1, 2, 3, 4, 5];arr.$insert(-2, "a", "b", "c"); // => [1, 2, 3, 4, "a", "b", "c", 5] $removeValue
示例代码1: var arr = [1, 2, 2, 3, 3, 3];arr.$removeValue(2); // arr => [1, 3, 3, 3] $remove
示例代码1: var arr = [1, 2, 3, 4, 5];arr.$remove(2); // arr => [1, 2, 4, 5] 示例代码2: var arr = [1, 2, 3, 4, 5];arr.$remove(-2); // arr => [1, 2, 3, 5] $drop
示例代码1: var arr = [1, 2, 3, 4, 5];arr.$drop(1); // arr => [1, 2, 3, 4]arr.$drop(1); // arr => [1, 2, 3]arr.$drop(1); // arr => [1, 2] 示例代码2: var arr = [1, 2, 3, 4, 5];var dropped = arr.$drop(3); // arr => [1, 2] dropped => [3, 4, 5] $removeIf
示例代码1: var arr = [ 1, 2, 3, 4, 5];arr.$removeIf(function (k, v) { return (v >= 3);});// arr => [1, 2] 示例代码2: var arr = [ 1, 2, 3, 4, 5];arr.$removeIf(function (k, v) { return true;});// arr => [] $keepIf
示例代码1: var arr = [ 1, 2, 3, 4, 5];arr.$keepIf(function (k, v) { return (v >= 3);})// arr => [3, 4, 5] $clear
示例代码1: var arr = [1, 2, 3];arr.$clear(); // arr => [] $replace
示例代码1: var arr = [ 1, 2, 3, 4, 5];arr.$replace(); // arr => [1, 2, 3, 4, 5]arr.$replace([]); // arr => []arr.$replace(["a", "b", "c"]); // arr => ["a", "b", "c"] $unique
示例代码1: var arr = [1, 2, 2, 3, 3, 3];arr.$unique(); // arr => [1, 2, 3] 示例代码2: var arr = [1, 1, 2, 2, 3, 3, 4, 4];arr.$unique(function (k, v) { if (v >= 3) return 3; if (v <= 2) return 2;});// 此时要比较的值变成[2, 2, 2, 2, 3, 3, 3, 3]// arr => [1, 3] $set
示例代码1: var arr = [1, 2, 3];arr.$set(1, "a"); // arr => [1, "a", 3]arr.$set(4, "a"); // arr不变,因为4已经超出数组长度 $sort
示例代码1: var arr = [4, 2, 5, 3, 1];arr.$sort(function (v1, v2) { return (v1 - v2) ;});//arr => [1, 2, 3, 4, 5] 示例代码2(实现倒排序): var arr = [4, 2, 5, 3, 1];arr.$sort(function (v1, v2) { return -(v1 - v2) ;});//arr => [5, 4, 3, 2, 1] 示例代码3(实现随机排序): var arr = [4, 2, 5, 3, 1];arr.$sort(function (v1, v2) { return Math.random() - 0.5;});//arr => [2, 4, 5, 1, 3] $rsort
示例代码同 $asort
$arsort
$asc
示例代码1: var arr = [ { "name": "Libai", "age": 24 }, { "name": "Zhangsan", "age": 22 }, { "name": "Wanger", "age": 23 }];arr.$asc("age"); 此时的 [ { "name": "Zhangsan", "age": 22 }, { "name": "Wanger", "age": 23 }, { "name": "Libai", "age": 24 }] $desc
示例代码1: var arr = [ { "name": "Libai", "age": 24 }, { "name": "Zhangsan", "age": 22 }, { "name": "Wanger", "age": 23 }];arr.$desc("age"); 此时的 [ { "name": "Libai", "age": 24 }, { "name": "Wanger", "age": 23 }, { "name": "Zhangsan", "age": 22 }] $swap
示例代码1: arr = [1, 2, 3];arr.$swap(0, 2); // arr => [3, 2, 1] $shuffle
示例代码1: var arr = [1, 2, 3];arr.$shuffle(); // arr => [2, 3, 1]arr.$shuffle(); // arr => [1, 3, 2] $contains
示例代码1: [1, 2, 3].$contains(3); // => true[1, 2, 3].$contains(4); // => false[1, 2, 3].$contains(null); // => false $include
$each
示例代码1: [1, 2, 3].$each(function (k, v) { console.log( "index:" + k + " v:" + v );}); 输出: index:0 v:1index:1 v:2index:2 v:3 $get
示例代码1: [1, 2, 3].$get(0); // => 1[1, 2, 3].$get(2); // => 3[].$get(0); // => null $getAll
示例代码1: var arr = [1, 2, 3, 4, 5];var newArr = arr.$getAll(); // newArr => []newArr = arr.$getAll(0, 2, 4); // newArr => [1, 3, 5]newArr = arr.$getAll(0, 2, 4, 6, 8); // newArr => [1, 3, 5] 因为6和8超出索引范围newArr = arr.$getAll(0, 2, [3, 4]); // newArr => [1, 3, 4, 5] $first
示例代码1: [1, 2, 3].$first(); // => 1[].$first(); // => null $last
示例代码1: [1, 2, 3].$last(); // => 3[].$last(); // => null $isEmpty
示例代码1: [1, 2, 3].$isEmpty(); // => false[].$isEmpty(); // => true $all
示例代码1: [1, 2, 3].$all(function (k, v) { return v > 1;});// => false 示例代码2: [1, 2, 3].$all(function (k, v) { return v > 0;});// => true $any
示例代码1: [1, 2, 3].$any(function (k, v) { return v > 3;});// => false 示例代码2: [1, 2, 3, 4, 5].$any(function (k, v) { return v > 3;});// => true $map
示例代码1: [1, 2, 3].$map(function (k, v) { return v * v;});// => [1, 4, 9] 从v0.0.2起,可以使用Array.$nil来跳过某些不想返回的值: 全部评论
专题导读
上一篇:Fastify: Fastify 是一个高度专注于以最少开销和强大的插件架构提供最佳开发人员体验 ...发布时间:2022-03-23下一篇:sosnail: 生成随机数,随机字母,随机颜色,identicon头像,转化颜色等功能的有趣插件 ...发布时间:2022-03-23热门推荐
热门话题
阅读排行榜
|
请发表评论