数组方法和对象模拟数组的方法

数组方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <!--js数组-->
  9. <script>
  10. var num = 15;
  11. var data = ['a', 'b', 'c', ['x', 'y']];
  12. var res = [];
  13. // 转字符串,数字转2进制,省略则默认10进制
  14. res = num.toString(2);
  15. console.log(res);
  16. // 1111
  17. // 数组转字符串,',' 连接
  18. res = data.toString();
  19. console.log(res);
  20. // 'a,b,c,x,y'
  21. // 数组转字符串,分隔符连接(省略则默认 , 连接)
  22. res = data.join('-');
  23. console.log(res);
  24. // a-b-c-x,y
  25. // 尾部添加,返回新数组长度
  26. res = data.push([1, 2]);
  27. console.log(data, res);
  28. // ['a', 'b', 'c', ['x', 'y'], [1, 2]] 数组长度 5
  29. // 尾部删除,删除最后一个元素,返回被删除的元素
  30. res = data.pop();
  31. console.log(data, res);
  32. // ['a', 'b', 'c', ['x', 'y']] 被删除 [1, 2]
  33. // 头部添加,返回新数组长度
  34. res = data.unshift([1, 2, 3]);
  35. console.log(data, res);
  36. // [[1, 2, 3], 'a', 'b', 'c', ['x', 'y']] 数组长度 5
  37. // 头部删除,删除第一个元素,返回被删除的元素
  38. res = data.shift();
  39. console.log(data, res);
  40. // ['a', 'b', 'c', ['x', 'y']] 被删除 [1, 2, 3]
  41. // 删除替换,第2位置删除1个元素,'o', {p: 'p', q: 'q'} 添加到删除位置,返回被删除的元素数组
  42. res = data.splice(2, 1, 'o', {p: 'p', q: 'q'});
  43. console.log(data, res);
  44. // ['a', 'b', 'o', {p: 'p', q: 'q'}, ['x', 'y']] 被删除 ['c']
  45. // 截取,不改变现有数组,返回第3到第4(不含)之前的元素组成新数组
  46. res = data.slice(3, 4);
  47. console.log(data, res);
  48. // ['a', 'b', 'o', {p: 'p', q: 'q'}, ['x', 'y']] 返回 [{p: 'p', q: 'q'}]
  49. // 合并,不改变现有数组,返回新数组
  50. res = data.concat([1, 2], 3);
  51. console.log(data, res);
  52. // ['a', 'b', 'o', {p: 'p', q: 'q'}, ['x', 'y']] 返回 ['a', 'b', 'o', {p: 'p', q: 'q'}, ['x', 'y'], 1, 2, 3]
  53. // 遍历,为每个元素执行回调,无返回值
  54. data.forEach(function (item, i, arr) {
  55. if ('object' == typeof item && !Array.isArray(item))
  56. console.log(item, i);
  57. })
  58. // {p: "p", q: "q"} 3
  59. // 遍历,为每个元素执行回调,返回新数组
  60. res = data.map(function (item, i, arr) {
  61. return Array.isArray(item);
  62. })
  63. console.log(res);
  64. // 返回 [false, false, false, flase, true]
  65. // 遍历,为每个元素执行回调,返回布尔是否全部为真
  66. res = data.every(function (item, i, arr) {
  67. return Array.isArray(item);
  68. })
  69. console.log(res);
  70. // false
  71. // 遍历,为每个元素执行回调,返回布尔是否有一个为真
  72. res = data.some(function (item, i, arr) {
  73. return Array.isArray(item);
  74. })
  75. console.log(res);
  76. // true
  77. // 过滤,返回过滤后的新数组
  78. res = data.filter(function (item, i, arr) {
  79. return Array.isArray(item);
  80. })
  81. console.log(res);
  82. // 返回 [['x', 'y']]
  83. // 归并,迭代所有项,返回最终值
  84. res = data.reduce(function (prev, curr, i, arr) {
  85. if (Array.isArray(curr)) {
  86. prev.push(curr);
  87. }
  88. return prev;
  89. }, [])
  90. console.log(res);
  91. // [['x', 'y']]
  92. // 从第3位置查找元素首次出现的位置索引
  93. res = data.indexOf('o', 3);
  94. console.log(res);
  95. // -1
  96. // 同上,从后往前查
  97. res = data.lastIndexOf('o');
  98. console.log(res);
  99. // 2
  100. // 查找值,查找符合条件的首个元素,未找到返回 undefined
  101. res = data.find(function (item, i, arr) {
  102. return 'string' == typeof item;
  103. });
  104. console.log(res);
  105. // a
  106. // 查找索引,查找符合条件的首个元素的位置索引,未找到返回 -1
  107. res = data.findIndex(function (item, i, arr) {
  108. return 'string' == typeof item;
  109. });
  110. console.log(res);
  111. // 0
  112. // 反转
  113. data.reverse();
  114. console.log(data);
  115. // [['x', 'y'], {p: 'p', q: 'q'}, 'o', 'b', 'a']
  116. // 排序
  117. data.sort();
  118. console.log(data);
  119. // [{p: 'p', q: 'q'}, 'a', 'b', 'o', ['x', 'y']]
  120. // unshift, shift, push, pop, sort, reverse, splice 会改变原始数组
  121. </script>
  122. </body>
  123. </html>

数组方法

对象模拟数组方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <!--对象模拟数组-->
  9. <script>
  10. function Myarray() {
  11. this.length = arguments.length;
  12. for (var i = 0; i < arguments.length; i++) {
  13. this[i] = arguments[i];
  14. }
  15. // 获取数据
  16. this.get = function () {
  17. var data = [];
  18. for (var i = 0; i < this.length; i++) {
  19. data[i] = this[i];
  20. }
  21. return data;
  22. }
  23. // 设置数据
  24. this.set = function (data) {
  25. this.length = data.length;
  26. for (var i = 0; i < data.length; i++) {
  27. this[i] = data[i];
  28. }
  29. }
  30. // toString 转字符串
  31. this.toString = function () {
  32. var data = '';
  33. for (var i = 0; i < this.length; i++) {
  34. if (data) data += ',';
  35. data += this[i];
  36. }
  37. return data;
  38. }
  39. // join 连接
  40. this.join = function (sep = ',') {
  41. var data = '';
  42. for (var i = 0; i < this.length; i++) {
  43. if (data) data += sep;
  44. data += this[i];
  45. }
  46. return data;
  47. }
  48. // push 尾部添加
  49. this.push = function (elem) {
  50. this[this.length] = elem;
  51. this.length++;
  52. return this.length;
  53. }
  54. // pop 尾部弹出
  55. this.pop = function () {
  56. var popData = this[this.length - 1];
  57. delete this[this.length - 1];
  58. this.length--;
  59. return popData;
  60. }
  61. // unshift 头部添加
  62. this.unshift = function (elem) {
  63. for (var i = this.length; i > 0; i--) {
  64. this[i] = this[i - 1];
  65. }
  66. this[0] = elem;
  67. this.length++;
  68. return this.length;
  69. }
  70. // shift 头部弹出
  71. this.shift = function () {
  72. var shiftData = this[0];
  73. for (var i = 0; i < this.length; i++) {
  74. this[i] = this[i + 1];
  75. }
  76. delete this[this.length - 1];
  77. this.length--;
  78. return shiftData;
  79. }
  80. // splice 删除替换
  81. this.splice = function (start = 0, count = 0, replace = null) {
  82. var delData = [];
  83. for (var i = 0; i < count; i++) {
  84. delData[i] = this[start + i];
  85. delete this[start + i];
  86. }
  87. if (replace) {
  88. for (var i = this.length + arguments.length - 2; i > start; i--) {
  89. this[i] = this[i - 1];
  90. }
  91. for (var i = 0; i < arguments.length - 2; i++) {
  92. this[start + i] = arguments[i + 2];
  93. this.length++;
  94. }
  95. }
  96. var data = [], j = 0;
  97. for (var i = 0; i < this.length; i++) {
  98. if (undefined === this[i]) continue;
  99. data[j] = this[i];
  100. j++;
  101. }
  102. this.set(data);
  103. return delData;
  104. }
  105. // slice 截取
  106. this.slice = function (start, pos) {
  107. var data = [], j = 0;
  108. for (var i = start; i < pos; i++) {
  109. data[j] = this[i];
  110. j++;
  111. }
  112. return data;
  113. }
  114. // concat 合并
  115. this.concat = function () {
  116. var data = this.get(), j = data.length;
  117. for (var i = 0; i < arguments.length; i++) {
  118. data[j] = arguments[i];
  119. j++;
  120. }
  121. return data;
  122. }
  123. // forEach 遍历
  124. this.forEach = function (fn) {
  125. for (var i = 0; i < this.length; i++) {
  126. fn(this[i], i, this);
  127. }
  128. }
  129. // map 遍历
  130. this.map = function (fn) {
  131. var data = [];
  132. for (var i = 0; i < this.length; i++) {
  133. data[i] = fn(this[i], i, this);
  134. }
  135. return data;
  136. }
  137. // every 遍历
  138. this.every = function (fn) {
  139. var data = true;
  140. for (var i = 0; i < this.length; i++) {
  141. data = data && fn(this[i], i, this);
  142. }
  143. return data;
  144. }
  145. // some 遍历
  146. this.some = function (fn) {
  147. var data = false;
  148. for (var i = 0; i < this.length; i++) {
  149. data = data || fn(this[i], i, this);
  150. }
  151. return data;
  152. }
  153. // filter 过滤
  154. this.filter = function (fn) {
  155. var data = [], j = 0;
  156. for (var i = 0; i < this.length; i++) {
  157. if (fn(this[i], i, this)) {
  158. data[j] = this[i];
  159. j++;
  160. }
  161. }
  162. return data;
  163. }
  164. // reduce 归并
  165. this.reduce = function (fn, init = '') {
  166. var data = init, j = 0;
  167. for (var i = 0; i < this.length; i++) {
  168. data = fn(data, this[i], i, this);
  169. }
  170. return data;
  171. }
  172. // indexOf 查找索引
  173. this.indexOf = function (value, pos = 0) {
  174. for (var i = pos; i < this.length; i++) {
  175. if (value == this[i]) return i;
  176. }
  177. return -1;
  178. }
  179. // lastIndexOf 倒着查找
  180. this.lastIndexOf = function (value, pos = 0) {
  181. for (var i = this.length - 1; i >= pos; i--) {
  182. if (value == this[i]) return i;
  183. }
  184. return -1;
  185. }
  186. // find 查找元素
  187. this.find = function (fn) {
  188. for (var i = 0; i < this.length; i++) {
  189. if (fn(this[i], i, this)) return this[i];
  190. }
  191. return undefined;
  192. }
  193. // findIndex 查找索引
  194. this.findIndex = function (fn) {
  195. for (var i = 0; i < this.length; i++) {
  196. if (fn(this[i], i, this)) return i;
  197. }
  198. return -1;
  199. }
  200. // reverse 反转
  201. this.reverse = function () {
  202. var data = [], j = 0;
  203. for (var i = this.length - 1; i >= 0; i--) {
  204. data[j] = this[i];
  205. j++;
  206. }
  207. this.set(data);
  208. }
  209. // sort 排序
  210. this.sort = function (fn) {
  211. var tmp;
  212. for (var i = 0; i < this.length; i++) {
  213. if (('function' === typeof fn && fn(this[i], this[i + 1])) || 'undefined' === typeof fn) {
  214. for (var j = i + 1; j < this.length; j++) {
  215. if (this[i] > this[j]) {
  216. tmp = this[i];
  217. this[i] = this[j];
  218. this[j] = tmp;
  219. }
  220. }
  221. } else {
  222. for (var j = i + 1; j < this.length; j++) {
  223. if (this[i] < this[j]) {
  224. tmp = this[i];
  225. this[i] = this[j];
  226. this[j] = tmp;
  227. }
  228. }
  229. }
  230. }
  231. }
  232. // max 最大值
  233. this.max = function () {
  234. var data = this[0];
  235. for (var i = 1; i < this.length; i++) {
  236. if (data < this[i]) data = this[i];
  237. }
  238. return data;
  239. }
  240. // min 最小值
  241. this.min = function () {
  242. var data = this[0];
  243. for (var i = 1; i < this.length; i++) {
  244. if (data > this[i]) data = this[i];
  245. }
  246. return data;
  247. }
  248. }
  249. var data = new Myarray('a', 'b', 'c', ['x', 'y']);
  250. // 数组转字符串,',' 连接
  251. res = data.toString();
  252. console.log(res);
  253. // 'a,b,c,x,y'
  254. // 数组转字符串,分隔符连接(省略则默认 , 连接)
  255. res = data.join('-');
  256. console.log(res);
  257. // a-b-c-x,y
  258. // 尾部添加,返回新数组长度
  259. res = data.push([1, 2]);
  260. console.log(data.get(), res);
  261. // ['a', 'b', 'c', ['x', 'y'], [1, 2]] 数组长度 5
  262. // 尾部删除,删除最后一个元素,返回被删除的元素
  263. res = data.pop();
  264. console.log(data.get(), res);
  265. // ['a', 'b', 'c', ['x', 'y']] 被删除 [1, 2]
  266. // 头部添加,返回新数组长度
  267. res = data.unshift([1, 2, 3]);
  268. console.log(data.get(), res);
  269. // [[1, 2, 3], 'a', 'b', 'c', ['x', 'y']] 数组长度 5
  270. // 头部删除,删除第一个元素,返回被删除的元素
  271. res = data.shift();
  272. console.log(data.get(), res);
  273. // ['a', 'b', 'c', ['x', 'y']] 被删除 [1, 2, 3]
  274. // 删除替换,第2位置删除1个元素,'o', {p: 'p', q: 'q'} 添加到删除位置,返回被删除的元素数组
  275. res = data.splice(2, 1, 'o', {p: 'p', q: 'q'});
  276. console.log(data.get(), res);
  277. // ['a', 'b', 'o', {p: 'p', q: 'q'}, ['x', 'y']] 被删除 ['c']
  278. // 截取,不改变现有数组,返回第3到第4(不含)之前的元素组成新数组
  279. res = data.slice(3, 4);
  280. console.log(data.get(), res);
  281. // ['a', 'b', 'o', {p: 'p', q: 'q'}, ['x', 'y']] 返回 [{p: 'p', q: 'q'}]
  282. // 合并,不改变现有数组,返回新数组
  283. res = data.concat([1, 2], 3);
  284. console.log(data.get(), res);
  285. // ['a', 'b', 'o', {p: 'p', q: 'q'}, ['x', 'y']] 返回 ['a', 'b', 'o', {p: 'p', q: 'q'}, ['x', 'y'], [1, 2], 3]
  286. // 遍历,为每个元素执行回调,无返回值
  287. data.forEach(function (item, i, arr) {
  288. if ('object' == typeof item && !Array.isArray(item))
  289. console.log(item, i);
  290. })
  291. // {p: "p", q: "q"} 3
  292. // 遍历,为每个元素执行回调,返回新数组
  293. res = data.map(function (item, i, arr) {
  294. return Array.isArray(item);
  295. })
  296. console.log(res);
  297. // 返回 [false, false, false, flase, true]
  298. // 遍历,为每个元素执行回调,返回布尔是否全部为真
  299. res = data.every(function (item, i, arr) {
  300. return Array.isArray(item);
  301. })
  302. console.log(res);
  303. // false
  304. // 遍历,为每个元素执行回调,返回布尔是否有一个为真
  305. res = data.some(function (item, i, arr) {
  306. return Array.isArray(item);
  307. })
  308. console.log(res);
  309. // true
  310. // 过滤,返回过滤后的新数组
  311. res = data.filter(function (item, i, arr) {
  312. return Array.isArray(item);
  313. })
  314. console.log(res);
  315. // 返回 [['x', 'y']]
  316. // 归并,迭代所有项,返回最终值
  317. res = data.reduce(function (prev, curr, i, arr) {
  318. if (Array.isArray(curr)) {
  319. prev.push(curr);
  320. }
  321. return prev;
  322. }, [])
  323. console.log(res);
  324. // [['x', 'y']]
  325. // 从第3位置查找元素首次出现的位置索引
  326. res = data.indexOf('o', 3);
  327. console.log(res);
  328. // -1
  329. // 同上,从后往前查
  330. res = data.lastIndexOf('o');
  331. console.log(res);
  332. // 2
  333. // 查找值,查找符合条件的首个元素,未找到返回 undefined
  334. res = data.find(function (item, i, arr) {
  335. return 'string' == typeof item;
  336. });
  337. console.log(res);
  338. // a
  339. // 查找索引,查找符合条件的首个元素的位置索引,未找到返回 -1
  340. res = data.findIndex(function (item, i, arr) {
  341. return 'string' == typeof item;
  342. });
  343. console.log(res);
  344. // 0
  345. // 反转
  346. data.reverse();
  347. console.log(data.get());
  348. // [['x', 'y'], {p: 'p', q: 'q'}, 'o', 'b', 'a']
  349. // 排序
  350. data.sort();
  351. console.log(data.get());
  352. // [{p: 'p', q: 'q'}, 'a', 'b', 'o', ['x', 'y']]
  353. // 最大最小值
  354. var numArr = new Myarray(4, 2, 8, 5, 7, 1);
  355. console.log(numArr.max(), numArr.min());
  356. </script>
  357. </body>
  358. </html>

对象模拟数组方法

更多相关文章

  1. js对象模拟数组
  2. JavaScript 实现简单数组排序,翻转,取较大值和较小值
  3. md5加密与数组函数
  4. vuex的commit没有返回值,数据创建后,检查是否成功创建的一个办法
  5. 为什么说for...of是JS中的一颗宝石
  6. 循环语句 超级全局变量 及 cURL函数
  7. 实现数组去重的 9 种高阶方法
  8. php多维数组创建及遍历
  9. json数据类型,安装MySQL

随机推荐

  1. Android中集成QQ登陆和QQ空间分享
  2. Android(安卓)权限中文说明
  3. android socket通信(下)
  4. android layout属性简介
  5. Android中几种数据结构使用
  6. android 隐藏键盘 ----- 断点记录
  7. 一 Android Camera框架
  8. XML解析各种方式比较
  9. 如何低成本打造品牌Android软件?
  10. Android(安卓)studio 43 文件存储到sdcar