js数组方法

1.join()和split()
join()可选择一个参数(数组转字符串)

var arr = ["a", "b", "c", "e", "f"]
var res = arr.join() // 
var res1 = arr.join("~") // 
console.log(res)//a,b,c,e,f
console.log(res1)//a~b~c~e~f
console.log(arr)// ["a", "b", "c", "e"]

split()两个参数:1.必须选择一个参数 2.可选。该参数可指定返回的数组的最大长度。
将字符串以指定的分隔符分割成数组(字符串转数组)

var str = "a,b,c,e,f"
var str1 = "a~b~c~e~f"
var res = str.split(",",3) // 
var res1 = str1.split("~") // 
console.log(res)//["a", "b", "c"]
console.log(res1)//["a", "b", "c", "e", "f"]

2.push()和pop()
push(): 可以接收任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度。

var arr = ["a","b","c"]
var res = arr.push("e","f")//5
console.log(arr)//["a", "b", "c", "e", "f"]

pop():数组末尾移除最后一项,减少数组的 length 值,然后返回移除的项。

var arr = ["a", "b", "c", "e", "f"]
var res = arr.pop() // f
console.log(arr)// ["a", "b", "c", "e"]

3.foreach()和map()
foreach()参数自定义可以有三个参数如下

var arr = ["a", "b", "c", "e", "f"]
arr.forEach((item,index,a)=>{
        console.log(item);//数组中的每一项
        console.log(index);//数组中每一项的下表
         console.log(a);//数组本身
} )

map()参数自定义可以有三个参数如下
这个方法需要在处理每一项item之后return返回参数

var arr = ["a", "b", "c", "e", "f"]
var res =  arr.map((item,index,a)=>{
        console.log(item);//数组中的每一项
        console.log(index);//数组中每一项的下表
         console.log(a);//数组本身
        if(item=="b"){
              return "B"
        }else{
              return item
        }
} )
console.log("arr:",arr);//arr:["a", "b", "c", "e", "f"]
console.log("res:",res);res:["a", "B", "c", "e", "f"]

4.reduce()和reduceRight()
reduce()参数自定义可以有四个参数如下
这个方法需要在处理每一项item之后return返回参数
(reduceRight()的循环从最后一个开始,其他相同)
init可选初始值

var arr = ["a", "b", "c", "e", "f"]
var init = "初始值"
arr.reduce((prev,item,index,a)=>{
        console.log(prev)//数组中的前一项的值
        console.log(item);//数组中的每一项的值
        console.log(index);//数组中当前项的下标
        console.log(a);//数组本身
        return item
},init)

5.sort()
sort()方法比较的是把数组中每一项转字符串后的值

var arr = ["f", "e", "c", "b", "a"]
var res = arr.sort() 
console.log("arr:",arr);//arr:["a", "b", "c", "e", "f"]
console.log("res:",res);res:["a", "b", "c", "e", "f"]

sort()方法可以添加一个比较函数用来排序
这个函数可以自定义

function compare(value1, value2) {
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
}
//var arr = [7,13,5,12,6]
var arr = ["7","13","5","12","6"]//对字符串无效
var res = arr.sort(compare)
console.log(res)//[5, 6, 7, 12, 13]
console.log(arr)// [5, 6, 7, 12, 13]

对字符串有效


function compare(value1,value2) {
  var value1 =  Number(value1)
  var value2 = Number(value2)
  if (value1 < value2) {
  return -1;
  } else if (value1 > value2) {
  return 1;
  } else {
  return 0;
  }
  }
  //var arr = [7,13,5,12,6]
  var arr = ["7","13","5","12","6"]//对字符串有效
  var res = arr.sort(compare)
  console.log(res)// ["5", "6", "7", "12", "13"]//返回的还是字符串类型
  console.log(arr)//  ["5", "6", "7", "12", "13"]

6.shift()和unshift()
shift():删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined 。

var arr = ["a", "b", "c", "e", "f"]
var res = arr.shift() 
console.log(res)//a
console.log(arr)// [ "b", "c", "e","f",]

unshift:将参数添加到原数组开头,并返回数组的长度 。

var arr = ["a", "b", "c", "e", "f"]
var res = arr.unshift("1","a") 
console.log(res)//6
console.log(arr)//["1", "a", "b", "c", "e", "f"]

7.reserver()
reserver()翻转数组中的顺序

var arr = ["a", "b", "c", "e", "f"]
var res = arr.reserver()
console.log(res)//["f", "e", "c", "b", "a"]
console.log(arr)//["f", "e", "c", "b", "a"]

8.concat()
concat() :将参数添加到原数组中。这个方法会先创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组。
在没有给 concat()方法传递参数的情况下,它只是复制当前数组并返回副本。

var arr = ["a", "b", "c", "e","d", "f"]
var res = arr.concat("g",["h","i"])
console.log(arr)// ["a", "b", "c", "d","e", "f"]
console.log(res)//["a", "b", "c", "e", "d","f", "g", "h", "i"]
//多维数组
var arr = ["a", "b", "c", "d",["e", "f"]]
var res = arr.concat("g",["h",["i","j"]])
console.log(arr)// ["a", "b", "c", "e", "f"]
console.log(res)//[ "a", "b", "c", ["e", "f"] ,"g","h",["i","j"] ]

9.slice()
slice():返回从原数组中指定开始下标到结束下标之间的项组成的新数组。slice()方法可以接受一或两个参数,即要返回项的起始和结束位置。
在只有一个参数的情况下, slice()方法返回从该参数指定位置开始到当前数组末尾的所有项。
如果有两个参数,该方法返回起始和结束位置之间的项,但不包括结束位置的项。

var arr = ["a", "b", "c", "e", "f","g"]
var arr0 = arr.slice(1);
var arr1 = arr.slice(1,4);
var arr2 = arr.slice(1,-2);
var arr3 = arr.slice(-4,-1);
console.log(arr); //["a", "b", "c", "e", "f", "g"]
console.log(arr0); //["b", "c", "e", "f", "g"]
console.log(arr1); // ["b", "c", "e"]
console.log(arr2); // ["b", "c", "e"]   
console.log(arr3); // ["c", "e", "f"]

10.splice()
splice():可以实现删除、插入和替换方法,并且始终都会返回一个数组,该数组中包含从原始数组中删除的项,如果没有删除任何项,则返回一个空数组。
删除:可以删除任意数量的项,只需指定 2 个参数:要删除的第一项的位置和要删除的项数。

var arr = ["a", "b", "c", "e", "f","g"]
var res = arr.splice(1,5)
console.log(res); // ["b", "c", "e", "f", "g"]
console.log(arr); // ["a"]

插入:只需提供 3 个参数:起始位置、 0(要删除的项数)和要插入的项。

var arr = ["a", "b", "c", "e", "f","g"]
var res =  arr.splice(3,0,"h","i")
console.log(arr); // []
console.log(arr); // ["a", "b", "c", "h", "i", "e", "f", "g"]

替换:只需指定 3 个参数:起始位置、要删除的项数和要插入的任意数量的项。
(插入的项数不必与删除的项数相等)
并且返回删除的项

var arr = ["a", "b", "c", "d","e", "f","g"]
 var res = arr.splice(3,2,"h","i")
console.log(res ); // ["d", "e"]
console.log(arr); // ["a", "b", "c", "h", "i", "f", "g"]

11.indexOf()和lastIndexOf()
indexOf()需要提供两个参数:
从数组的开头(位置 0)开始向后查找。
1.要查找的项
2.(可选的)表示查找起点位置的索引。其中, 从数组的开头(位置 0)开始向后查找。
lastIndexOf()也需要提供两个参数(同上),但是从数组的结尾开始向前查找。

var arr = ["a", "b", "c", "d","e", "f","g"]
 var res = arr.indexOf("c")
 var res1 = arr.indexOf("d",2)
 var res2 = arr.indexOf("d",4)
 var res3 = arr.indexOf("d",5)
console.log(res); // 2
console.log(res1); // 3
console.log(res2); // -1
console.log(res3); // -1 表示没有查找到
console.log(arr); // ["a", "b", "c", "d", "e", "f", "g"]

12.filter()
filter()函数满足过滤条件组成的数组
注意: 不会对空数组进行检测。
注意:不会改变原始数组。

var arr = ["a", "b", "c", "d","e", "f","g"]

var res = arr.filter((item,index,a)=>{
        console.log(item);//数组中的每一项
        console.log(index);//数组中每一项的下表
         console.log(a);//数组本身
         //console.log(this);//这个第四个参数不明白具体是做什么的
        return item>"c"?true:false
} ,{  x:"这是X的值"})
console.log(arr); // ["a", "b", "c", "d", "e", "f", "g"]//不改变原来的数组
console.log(res); // ["d", "e", "f", "g"]

13.every()和some()
every():判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回true。

var arr = [ "b", "c", "d","e", "f","g"]
var res = arr.every((item,index,a)=>{
        console.log(item);//数组中的每一项
        console.log(index);//数组中每一项的下表
         console.log(a);//数组本身
        return item>"a"
} ,{  x:"这是X的值"})
console.log(arr); // ["a", "b", "c", "d", "e", "f", "g"]//不改变原来的数组
console.log(res); // true

some()判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。

var arr = [ "b", "c", "d","e", "f","g"]
var res = arr.some((item,index,a)=>{
        console.log(item);//数组中的每一项
        console.log(index);//数组中每一项的下表
         console.log(a);//数组本身
        return item>"f"
} ,{  x:"这是X的值"})
console.log(arr); // ["a", "b", "c", "d", "e", "f", "g"]//不改变原来的数组
console.log(res); // true

  1. for in和for of
  var arr = [ "b", "c", "d","e", "f","g"]
  for(let item in arr){ // item为index,数组索引
      console.log(item); 
  }
  var arr = [{a:"1"},{b:"2"},{c:"3"}]
  for(let item in arr){ // item为index,数组索引
      console.log(item); 
  }
  var arr =  [ "b", "c", "d","e", "f","g"]
   for(let item of arr){ // item为value
     console.log(item); //a遍历为每一项的键
   }
  var arr = [{a:"1"},{b:"2"},{c:"3"}]
   for(let item of arr){ // item为value
     console.log(item); //{a: "1"}遍历为每一项
   }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容