首页 > 其他分享 >JS数组方法大全

JS数组方法大全

时间:2022-09-06 20:13:07浏览次数:82  
标签:const log array1 JS 数组 console Array prototype 大全

1.  at()方法     Array.prototype.at()

at() 方法接收一个整数值并返回该索引的项目,允许正数和负数。负整数从数组中的最后一个项目开始倒数。

const array1 = [5, 12, 8, 130, 44];

let index = 2;

console.log(array1.at(index));
// 2

index = -3;

console.log(array1.at(index));
//8

 

2.  concat()方法   Array.prototype.concat()

concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]

3.  every()方法  Array.prototype.every()

every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// expected output: true

4.fill()方法    Array.prototype.fill()    语法:fill(value, start, end)

fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。

const array1 = [1, 2, 3, 4];

// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]

// fill with 5 from position 1
console.log(array1.fill(5, 1));
// expected output: [1, 5, 5, 5]

console.log(array1.fill(6));
// expected output: [6, 6, 6, 6]

5.  filter()    Array.prototype.filter()

filter() 方法创建一个新数组,其包含通过所提供函数实现的测试的所有元素。

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

6.  filter()方法    Array.prototype.filter()

filter() 方法创建一个新数组,其包含通过所提供函数实现的测试的所有元素。

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

7.find()方法   Array.prototype.find()

find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined

const array1 = [5, 12, 8, 8, 44];

const found = array1.find(element => element > 10);

console.log(found);
// expected output: 12

8. findIndex()方法   Array.prototype.findIndex()

findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));
// expected output: 3

9.findLast() 方法   Array.prototype.findLast()

findLast() 方法返回数组中满足提供的测试函数条件的最后一个元素的值。如果没有找到对应元素,则返回 undefined

const array1 = [5, 12, 50, 130, 44];

const found = array1.findLast((element) => element > 2);
if(found===undefined){
console.log("没有满足条件");
}else{
console.log(found);
}

//console.log(found);
// expected output: 44

10. forEach()方法  Array.prototype.forEach()

forEach() 方法对数组的每个元素执行一次给定的函数。

运用案例:

searchPatient(state, x) {
// state.searchResult = x
function expireTime(time) {
Date.prototype.format = function (format) {
......
}
let date = new Date(time);
let expireTime = date.format("yyyy-MM-dd");
return expireTime
}
//x是一个数组里面有四个对象
x.forEach(item => {
//x的每一项的registrationDate重新赋值
item.registrationDate = expireTime(item.registrationDate)
});
state.searchResult = x
}

 

 //暂时先到这,后期再补一些。。   - .-

 

标签:const,log,array1,JS,数组,console,Array,prototype,大全
From: https://www.cnblogs.com/ruby888/p/16663151.html

相关文章

  • 前端JS-Day21
    client系列:获得可视区域的相关信息clientWidth和offsetWidth区别:clientWidth只包含内容和padding,offsetWidth包含内容和内外边框。  立即执行函数:无需调用,直接执行......
  • 【前端】JS json、字符串互转
    1、json字符串转json对象JSON.parse    2、json对象转json字符串JSON.stringify  ......
  • three.js实现简单的3D中国省份立体板块地图
    源码资料获取:https://github.com/huangchun0121/3D-example/tree/main/省份立体板块实现代码:点击查看代码<!DOCTYPEhtml><htmllang="en"><head><metacharse......
  • jsp大文件上传解决方案支持分片断点上传
    ​前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对Http协议较模糊,故这次采用渐进的方式来学习文件上传的原......
  • 可变数组
    可变数组上完翁恺老师的可变数组后发现并不是很理解,但是跟着敲了一遍,也有些许的感觉,下面就记录一下首先我们的确定可变数组需要些什么函数:Arrayarray_create(intin......
  • C#:初识结构体、数组、冒泡排序。
    代码:///<summary>///1.结构体与枚举、变量相似,都是自定义一种新的数据的类型///2.结构体中的不称为变量,被称为是字段。,因为变量只可以储存一种数据,字段可以......
  • Js学习之------如何判断对象为空?
    1、JSON.stringify()JSON.stringify()可以把json对象转为json字符串只需要判断序列化后的对象是否全等于字符串“{}”即可2、Object.keys()ES6中Object.keys()方法,会把对象中......
  • 关于nodejs的一些笔记
    node.js和JavaScript还是有一定的渊源的简单来说,Nodejs就是运行在服务端的JavaScript浏览器有一个引擎比如谷歌的chrome里的叫做V8这个引擎可以翻译JavaScript脚本,然......
  • python requests.post() 请求中 json 和 data 的区别
    requests.post()请求中json和data的区别post请求中,可以使用data传递参数,也可以使用json传递参数。那么,两种方式有什么区别?1.如果参数为JSON数据,可以直接传入json参......
  • js之三级联动省市县
    1<!DOCTYPEhtml>2<htmllang="en">34<head>5<metacharset="UTF-8">6<metahttp-equiv="X-UA-Compatible"content="IE=edge">7......