javascript中常用方法

本文详细介绍了JavaScript中数组的各种操作方法,包括连接、删除、插入、排序等,并通过具体实例展示了如何使用这些方法来高效地处理数组数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Array
'use strict';
let cout = function(){
    console.log('arr1 :'+ arr1);
    console.log('arr2 :'+ arr2);
    for(let i=0;i<arguments.length;i+=1){
        console.log(arguments[i]);
    }
}
let arr1 = [1,2,13,15,23];
let arr2 = ['a','b','c'];
let arrCat = arr1.concat(arr2,'d');//c = [ 1, 2, 13, 15, 23, 'a', 'b', 'c', 'd' ]
console.log(arrCat.join());
//返回一个数组中元素组成的字符串,以‘,’隔开
//1,2,13,15,23,a,b,c,d
console.log(arrCat.join(''));
//没有分隔符
//12131523abcd
arrCat.pop();
//删除最后一个元素
//shift  删除第一个元素
arrCat.push(['d','e','f']);
//在末尾添加一个元素
//unshift 在头部添加元素
//[ 1, 2, 13, 15, 23, 'a', 'b', 'c', [ 'd', 'e', 'f' ] ]
var b=arrCat;//引用复制
b.reverse();
//数组反转
console.log(b);
//[ [ 'd', 'e', 'f' ], 'c', 'b', 'a', 23, 15, 13, 2, 1 ]
console.log(arrCat);
//[ [ 'd', 'e', 'f' ], 'c', 'b', 'a', 23, 15, 13, 2, 1 ]
b.sort();//默认转成字符串后排序
//[ 1, 13, 15, 2, 23, 'a', 'b', 'c', [ 'd', 'e', 'f' ] ]
b.sort(function(a,b){
    if(a===b){
        return 0;
    }
    if(typeof a === typeof b){
        return a<b?-1:1;
    }else{
        return typeof a < typeof b ? -1 : 1;
    }

})
//[ 1, 2, 13, 15, 23, [ 'd', 'e', 'f' ], 'a', 'b', 'c' ]
b.splice(0,2,9,[23,24]);
//第一参数为要删除的下标,第二参数为删除数,后面参数为要在此位置上添加的元素
//b === [ 9, [ 23, 24 ], 13, 15, 23, [ 'd', 'e', 'f' ], 'a', 'b', 'c' ]
let c = b.slice(1,2);
//第一参数为开始截取位置,第二参数为结束截取位置
//c = [ [ 23, 24 ] ]
  1. Function
    Function.apply(thisArg,argArray)
  2. Number

  3. Object
    Object.hasOwnProperty(name)

  4. RegExp
    regexp.exec(string)//返回数组
    regexp.test(string)//返回boolean
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值