es6数组处理方法整理

news/2024/7/10 14:27:50 标签: es6, js, 数组

数组

常用

  • .push() .pop()
  • .unshift() .shift()
  • .splice(i,n) 删除,原数组被从i删除n个元素,返回被删掉的元素
  • .slice(start,end) 获取子数组,原数组不变,返回切出来的元素
  • .concat()
  • .split() 字符串变数组
  • .sort() 排序
  • .reverse() 翻转

遍历处理

.map(callback)遍历处理后返回一个新数组

有return

let newArr=arr.map((value,index,array)=>{
 return
})

.forEach(callback)遍历处理

相当于for循环,无return

arr.forEach((value,index,array)=>{
	//todo
})

.filter(callback)遍历并按条件过滤,返回满足条件的元素组成的新数组

返回新数组

let arr1 = arr.filter( (value, index) => value<3)

遍历查询

.indexOf() 查找某个元素的索引值

若有重复的,则返回第一个查到的索引值若不存在,则返回 -1
.lastIndexOf(),倒着查

let index= arr.indexOf(value)

.find(callback) 找到第一个符合条件的数组成员

let value= arr.find((value, index, array) =>value > 3)

.findIndex(callback) 找到第一个符合条件的数组成员的索引值

let index= arr.findIndex((value, index, array) => value > 3)

初始化时填充

.fill(target, start, end) 使用给定的值,填充一个数组

返回新数组

let newArr=new Array(10).fill(0)

遍历判断

.includes() 判断是否包含给定的值

返回true、false

let bool= arr.includes(value)  

.every(callback)遍历判断数组的元素是否全满足条件,若每一个都满足则返回ture

返回true、false

let bool= arr.every( (value, index) =>value<3)

.some(callback)遍历判断数组的元素是否有一个满足条件,若每有则返回ture

返回true、false

let bool= arr.some( (value, index) =>value<3)

遍历获取

.keys() 遍历数组的索引

返回所有key,数组,也即[0,1,2,3…]

let arr2 = arr.keys()

.values() 遍历数组的值

返回所有值,数组,也即数组本身

let arr2 = arr.values()

.entries()遍历数组的索引和值

返回数组

let arr2 = arr.entries()

http://www.niftyadmin.cn/n/1503456.html

相关文章

mysql字符集小结

author&#xff1a;skate time:2013/04/09 mysql字符集小结 今天同事阿杰兄发现内部一台mysql测试服务器乱码&#xff0c;以前也记录过关于字符集的&#xff0c;今天再补充下 修改mysql的字符集和默认存储引擎 http://blog.csdn.net/wyzxg/article/details/7581415 查看库现有的…

vue v-for使用整理

v-for 普通数字 data:{}<p v-for"count in 4">第{{count}}个</p>普通数组 data:{list:[1,2,3,4] }<p v-for"(value,index) in list">--索引--{{index}} --值--{{value}}</p>普通对象 data:{obj:{a:1,b:2,c:3} }<p v-for&…

《OOC》笔记(2)——C语言实现trycatchfinally

本篇就偷个懒吧&#xff0c;实在打不起精神。 1 #ifndef _TRY_THROW_CATCH_H_2 #define _TRY_THROW_CATCH_H_3 4 #include <stdio.h>5 #include <setjmp.h>6 7 /* For the full documentation and explanation of the code below, please refer to8 * http://www.…

Java从零开始学十七(简单工厂)

简单工厂的实现 实现一个计算器&#xff1a;要求输入2个数&#xff0c;和运算符&#xff0c;得到结果 Operation类 package com.pb.demo1;public class Operation {private double num1;private double num2;public double getResult(double num1,double num2){double result0;…

vue插值--:class:style

html中使用变量 html中文本–>{{}} <span>Message: {{ msg }}</span> 属性值或布尔值–>v-bind <div :id"myId"></div> <button :disabled"btnDisable">Button</button> html表达式–>{{}} {{ number …

Linux基础知识---计划任务

计划任务所谓的计划任务是在未来某一特定的时间执行一次或多次特定的作业&#xff08;任务&#xff09;。实现无需人工干预的情况下执行作业。一、计划任务的分类根据计划任务在未来被执行的次数&#xff0c;分为一次性计划任务和周期性计划任务。一次性计划任务&#xff1a;只…

echart toolbox全屏显示

方案一&#xff1a;F11式的全屏 自定义toolbox工具&#xff1a; toolbox:{show: true,feature: {myFull: {show: true,title: 全屏查看,icon: "path://M432.45,595.444c0,2.177-4.661,6.82-11.305,6.82c-6.475,0-11.306-4.567-11.306-6.82s4.852-6.812,11.306-6.812C427.…

再思如何做好一个好的网站

最近一切进展都很慢。 网站的事情一切如常&#xff0c;周公解梦&#xff08;www.dlnulib.com&#xff09;依然处于惩罚期&#xff0c;没有了任何的排名。二飞日志&#xff08;www.erfeirizhi.com&#xff09;也因为上次的服务器关停事件&#xff0c;依然关在seo的小黑屋里。配资…