返回新的数组

  1. concat
  2. slice

    数组方法

    concat

  • 数组拼接

    1. var arr1 = ['a', 'b', 'c'];
    2. var arr2 = [1, 2, 3];
    3. var arr3 = arr1.concat(arr2);
    4. console.log(arr1,arr2,arr3)

    image.png

    toString

    1. var arr = ['1','2','3'];
    2. console.log(arr.toString());//1,2,3

    slice

  • 数组剪切

    1. var arr = ['1', 'a','b','c'];
    2. var a = arr.slice();
    3. console.log(a)//['1', 'a','b','c'];
    4. console.log(a===arr)//false
  1. slice(index) 从这个index开始截取,一直截取到最后。

    1. var arr = [1,2,3,4];
    2. var arr1 = arr.slice(1);//[2,3,4];
  2. slice(start,end) 从start 开始 end结束。开区间[1,3);

    1. var arr = [1, 2, 3, 4];
    2. var arr1 = arr.slice(1, 3);
    3. console.log(arr1)//[2,3];
  3. 和splice 一样 负数都从最后面开始数 -1;

    1. var arr = [1, 2, 3, 4];
    2. // 0 1 2 3 前面往后数
    3. // -4 -3 -2 -1 后面往前数
    4. var arr1 = arr.splice(-1,-3);//[]
    5. var arr2 = arr.splice(-3,-1);//[2, 3] //都是从左到右开始裁剪 slice(start,end) start< end;

    join split

  4. join 数组以字符拼接成字符串。

  5. split 字符串以什么字符为分隔符 分割成数组。

    1. var arr = ['a', 'b', 'c', 'd', 'e'];
    2. var str1 = arr.join();//a,b,c,d,e默认逗号隔开
    3. var str2 = arr.join('')//abcde
    4. var str3 = arr.join('-')//a-b-c-d-e

    split(a,b)

  6. a 是分隔符

  7. b 是分割的位数

    1. var arr = ['a', 'b', 'c', 'd', 'e'];
    2. var str1 = arr.join();//数组元素默认以,号拼接
    3. console.log(str1, str1.split(','));
    4. var str2 = arr.join('');//数组元素直拼接。
    5. console.log(str2, str2.split(''));
    6. var str3 = arr.join('-');//数组元素以-拼接
    7. cosnole.log(str2.split('-',3))//字符串以-为分隔符 截取长度为3

    image.png

    类数组

  8. 没有push 方法 说明不是继承了Array.prototype

  9. 类数组继承于对象
  10. 类数组可以转成数组

    1. function test(){
    2. var argArr = Array.prototype.slice.call(arguments);
    3. console.log(arguments,argArr);
    4. }
    5. test(1,2,3,4);

    image.png

    类数组的构成

  11. length

  12. splice
    1. var obj={
    2. 0:1,
    3. 1:2,
    4. length:2,
    5. splice:Array.prototype.splice
    6. }
    1. var obj={
    2. 0:1,
    3. 1:2,
    4. length:2,
    5. push:Array.prototype.push,
    6. splice:Array.prototype.splice
    7. }
    8. obj.push(3);
    image.png

    实现push

    1. Array.prototype.push= function(elem){
    2. this[this.length]=elem;//this 代表数组.
    3. this.length++;//数组长度++
    4. }

    笔试题

    1. var obj = {
    2. '2':3,
    3. '3':4,
    4. 'length':2,
    5. push:Array.prototype.push,
    6. splice:Array.prototype.splice
    7. }
    8. obj.push(1);//{'2':1,'3':4}
    9. obj.push(2);//{'2':1,'3':2}
    10. console.log(obj)
    image.png

作业

  1. Array.prototype.unique = function(){}//去重
  2. 封装myTypeof ```javascript //利用对象键名唯一性。 Array.prototype.unique = function(){ var origin = {}; for (let index = 0; index < this.length; index++) { origin[this[index]]=this[index]; } return Object.values(origin); }

/**/ Array.prototype.unique = function(){ var origin = {}, arr=[]; for (let index = 0; index < this.length; index++) { if(!origin[this.index]){ origin[this[index]]=’value’;// this[index]=0 存在false; arr.push(this[index]); } } return arr } /**/ Array.prototype.unique = function(){ var origin = {}, arr=[]; for (let index = 0; index < this.length; index++) { if(!origin.hasOwnProperty(this[index])){//解决了this[index]=0 的问题 origin[this[index]]=this[index]; arr.push(this[index]); } } return arr } /**/ function myTypeof (str){ /**

  1. * undefined, null ,string,boolean,function,number,object
  2. * array,object,object-number object-boolean object-string
  3. */
  4. var toStr = Object.prototype.toString.call(str),
  5. type=null,
  6. originArray=['undefined','string','boolean','number','function'],//typeof 能返回的数据类型。
  7. typeOfVal = typeof str,
  8. typeValObj = {
  9. '[object Object]':'object',
  10. '[object Array]':'array',
  11. '[object String]':'object-string',
  12. '[object Number]':'object-number',
  13. '[object Boolean]':'object-boolean',
  14. '[object Null]':'null',
  15. }//Object.prototype.toString()能返回的全部类型。
  16. if(originArray.indexOf(typeof str)!==-1){
  17. return typeOfVal
  18. }else{
  19. return typeValObj[toStr];
  20. }

} /**/ function originType (str){ var type = typeof(str), toStr = Object.prototype.toString, res = { ‘[object Object]’:’object’, ‘[object Array]’:’array’, ‘[object String]’:’object-string’, ‘[object Number]’:’object-number’, ‘[object Boolean]’:’object-boolean’, } if(str===null){ return ‘null’ }else if(type===’object’){ var ret = toStr.call(str); return res[ret]; }else{ return type; } }

```