[关闭]
@bluexiaowei 2016-09-04T13:58:07.000000Z 字数 2955 阅读 562

数组操作

javascript


@来源互联网
@整理: bluexiaowei


1. 数组去重

  1. Array.prototype.unique1 = function(arr) {
  2. var
  3. i,
  4. length = this.length,
  5. mapArray = [],
  6. hash = {};
  7. for (i = 0; i < length; i += 1) {
  8. !hash[this[i]] && (
  9. hash[this[i]] = true,
  10. mapArray.push(this[i])
  11. );
  12. }
  13. if(arr){
  14. for(i = 0, length = arr.length; i < length; i += 1) {
  15. !hash[arr[i]] && (
  16. hash[arr[i]] = true,
  17. mapArray.push(arr[i])
  18. );
  19. }
  20. }
  21. return mapArray;
  22. };
  23. Array.prototype.unique2 = function() {
  24. var
  25. i,
  26. length = this.length,
  27. mapArray = [];
  28. this.sort();
  29. for (i = 0; i < length; i += 1){
  30. this[i] != this[i + 1] && mapArray.push(this[i]);
  31. }
  32. return mapArray;
  33. }
  34. Array.prototype.unique3 = function() {
  35. var
  36. i,
  37. length = this.length;
  38. mapArray = [];
  39. for (i = 0; i < length; i += 1){
  40. mapArray.indexOf(this[i]) == -1 && mapArray.push(this[i]);
  41. }
  42. return mapArray;
  43. };
  44. Array.prototype.unique4 = function() {
  45. var
  46. i,
  47. length = this.length;
  48. mapArray = [this[0]];
  49. for (i = 1; i < length; i +=1) {
  50. this.indexOf(this[i]) == i && mapArray.push(this[i]);
  51. }
  52. return mapArray;
  53. }

2. 数组去同留异 true、去异留同 false

  1. function reserve(arr1, arr2, mode) {
  2. var
  3. i,
  4. length,
  5. key,
  6. mapArray = [],
  7. hash = {};
  8. for(i = 0, length = arr2.length; i < length; i += 1) {
  9. !hash[arr2[i]] && (
  10. hash[arr2[i]] = true,
  11. mapArray.push(arr2[i])
  12. );
  13. }
  14. arr2 = mapArray;
  15. mapArray = [];
  16. hash = {};
  17. for(i = 0, length = arr1.length; i < length; i += 1) {
  18. hash[arr1[i]] = true;
  19. }
  20. for(i = 0, length = arr2.length; i < length; i += 1) {
  21. if(mode) {
  22. hash[arr2[i]] && mapArray.push(arr2[i]);
  23. } else {
  24. hash[arr2[i]] ? delete hash[arr2[i]] : mapArray.push(arr2[i]);
  25. for(key in hash) {
  26. if(hash.hasOwnProperty(key)) {
  27. mapArray.push(key);
  28. }
  29. }
  30. }
  31. }
  32. return mapArray;
  33. }

3. 数组排序

  1. /*升序*/
  2. Array.sort();
  3. /*降序*/
  4. array = array.sort( function( a, b ) { return b - a } );
  5. /*乱序*/
  6. array = array.sort( function() { return Math.random() - 0.5 } );
  7. /* 选择排序 */
  8. Array.prototype.selectionSort = function() {
  9. var
  10. i,
  11. j,
  12. cache,
  13. index,
  14. length = this.length;
  15. for(i = 0; i < length; i += 1) {
  16. cache = this[i];
  17. index = i;
  18. for(j = i + 1; j < length; j += 1) {
  19. cache > this[j] && (
  20. cache = this[j],
  21. index = j
  22. )
  23. }
  24. this[index] = this[i];
  25. this[i] = cache;
  26. }
  27. return this;
  28. };
  29. /* 冒泡排序 */
  30. Array.prototype.bubbleSort = function() {
  31. var
  32. i,
  33. j,
  34. yes,
  35. cache,
  36. length = this.length;
  37. for(i = 0; ;i += 1) {
  38. yes = true;
  39. for(j = 0; j < length; j += 1) {
  40. this[j] > this[j + 1] && (
  41. cache = this[j + 1],
  42. this[j + 1] = this[j],
  43. this[j] = cache,
  44. yes = false
  45. );
  46. }
  47. if(yes) return this;
  48. }
  49. };
  50. /* 随机排序 */
  51. Array.prototype.shuffle = function() {
  52. var
  53. j,
  54. x,
  55. i = this.length;
  56. for(; i;) {
  57. j = parseInt(Math.random() * i);
  58. x = this[--i];
  59. this[i] = this[j];
  60. this[j] = x;
  61. };
  62. return this;
  63. };

3. 数组合并

  1. /*内置方法*/
  2. var arr = arr1.concat(arr2);
  3. /*优化*/
  4. var arr = Array.prototype.push.call(arr1,arr2);
  5. /*或者*/
  6. arr1.push.call(arr1,arr2);

4. 数组截断

  1. /*减小数组长度*/
  2. var
  3. arr = [1, 2, 3, 4, 5, 6, 7];
  4. console.log(arr);[1, 2, 3, 4, 5, 6, 7];
  5. arr.length = 3;
  6. console.log(arr);//[1, 2, 3]

6. 数组截取

  1. var arr, array = [ 1, 2, 3, 4, 5, 6 ];
  2. arr = array.slice();
  3. console.log( arr ); //[ 1, 2, 3, 4, 5, 6 ]
  4. arr = array.slice( 2, 5 );
  5. console.log( arr ); //[ 3, 4, 5 ]
  6. arr = array.slice( -2, 5 );
  7. console.log( arr ); //[ 5 ]
  8. arr = array.slice( -2, -1 );
  9. console.log( arr ); //[ ]
  10. arr = array.slice( -3 );
  11. console.log( arr ); //[ 4, 5, 6 ]

5. 深度克隆

javscript 中数组类型分两种,原始类型、对象类型。都可以用浅克隆。除了对象类型中的对象得用深度克隆。

  1. function clone(object){
  2. var
  3. key,
  4. newObj,
  5. obj = Object.prototype.toString.call(object).slice(8,-1);
  6. if(obj !== 'Object'){
  7. return newObj = object;
  8. } else if (window.JSON) {
  9. newObj = JSON.stringify(object);
  10. newObj = JSON.parse(newObj);
  11. return newObj;
  12. } else {
  13. for(key in object){
  14. newObj[key] = object[key];
  15. }
  16. return newObj;
  17. }
  18. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注