海诗网 知识百科 js数组常用方法(js数组排序方法)

js数组常用方法(js数组排序方法)

js数组常用方法, js数组的常用方法有哪些?不知道小伙伴们今天来看看边肖的分享吧!

数组是一种特殊的变量,可以一次存储多个值。JS中的数组不是数组,而是对象。js中的数组不同于其他语言中的数组。其实它不是数组,而是一个具有类数组特征的对象。

它只是将索引转换成一个字符串,并将其用作属性(键)。

1. Filter ()

例如:

我们希望获得该列表中所有小于或等于24岁的学生。我们需要使用过滤方法来过滤掉所有24岁以上的学生。

Output: const students=[{name: sam, age: 26, score: 80}, {name: john, age: 25, score: 86}, {name: dean, age: 20, score: 78}, {name: timothy, age: 18, {name: frank, age: 21, score: 67}] const filtered students=students.filter ((students)={//This filtering method just takes a function, which will have a student parameter. This parameter is each student/item in the array.//We need to return a true or false statement to determine whether we want to include them in the new array. return students. age=24//This line basically hides all students under the age of 24 in the new filteredStudents array}) Console.log (filtered students)//What we do is to return true or false for each item.

js数组排序方法

2、以map()为例:

Map()允许您获取一个数组,并将其转换为一个新数组。在这种情况下,新数组中的所有项目看起来略有不同。如果我们想知道每个学生的名字。我们可以通过使用map()获得一个名称数组。

输出:

const students=[{姓名:sam,年龄:26,分数:80},{姓名:john,年龄:25,分数:86},{姓名:dean,年龄:20,分数:78},{姓名:timothy,年龄:18,分数:95},{姓名:frank,年龄:21,分数:67 }]const student names=students。地图((学生)={//该方法采用单个函数,以学生/项目作为参数//这里我们只返回我们在*//******************/这可以通过数组中的任何内容来完成。

js数组排序方法

3.这个方法允许你在一个数组中查找一个单独的对象。这是一个简单的方法。

const students=[{姓名山姆,年龄:26,分数:80},{姓名:约翰,年龄:25,分数:86},{姓名:院长,年龄:20,分数:78},{姓名:提摩太,年龄:18,分数:95},{姓名:弗兰克,年龄:21,分数:67 }]const单个学生=学生。find((学生)={返回学生。name===John })控制台。日志(单个学生)/*控制台。日志(单个学生)会给在这个方法中,

js数组排序方法

const students=[{姓名:sam,年龄:26,分数:80},{姓名:john,年龄:25,分数:86},{姓名:dean,年龄:20,分数:78},{姓名:timothy,年龄:18,分数:95},{姓名:frank,年龄:21,分数:67 }]const hasundersagestudents=学生。有的((学生)={返校生。18岁})控制台。log(hasundersagestudents)/*这将返回假的,因为*/因此,

js数组排序方法

const students=[{姓名山姆,年龄:26,分数:80},{姓名:约翰,年龄:25,分数:86},{姓名:院长,年龄:20,分数:78},{姓名:提摩太,年龄:18,分数:95},{姓名:弗兰克,年龄:21,分数:67 }]const总分=学生。减少((当前总数,学生)={返回学生。当前总分},0)console.log(总分)在代码读取时,

js数组排序方法

const fruits=[ mango, apple, orange, pineapple, lemon]const includesApple=fruits.includes(apple)console.log(includesApple) //this will return true because apple is one of the items inside the array.

js数组常用方法,以上就是本文为您收集整理的js数组常用方法最新内容,希望能帮到您!更多相关内容欢迎关注。

本文来自网络,不代表海诗网立场,转载请注明出处:https://www.hallse.com/n/168167.html
      

唱鸭的耳返设置在哪里(监听耳机录音的耳返怎样设置)

发表回复
联系我们
联系我们

在线咨询: QQ交谈

邮箱: 3587015498@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们
微信扫一扫关注我们
关注微博
返回顶部