国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

js數組常用方法

來源:懂視網 責編:李贏贏 時間:2021-12-06 11:40:47
文檔

js數組常用方法

數組是一種特殊的變量,它能夠一次存放一個以上的值。js里的數組和其他語言中的數組是不同的,實際它并不是數組,而是一種array-like特性的對象。常用數組:1、filter;2、map;3、find;4、forEach;5、some;6、every;7、reduce;8、includes。
推薦度:
導讀數組是一種特殊的變量,它能夠一次存放一個以上的值。js里的數組和其他語言中的數組是不同的,實際它并不是數組,而是一種array-like特性的對象。常用數組:1、filter;2、map;3、find;4、forEach;5、some;6、every;7、reduce;8、includes。

js數組常用方法有哪些呢?不知道的小伙伴來看看小編今天的分享吧!

數組是一種特殊的變量,它能夠一次存放一個以上的值。JS里的"數組"不是數組,而是對象。js里的數組和其他語言中的數組是不同的,實際它并不是數組,而是一種array-like 特性的對象。它只是把索引轉化成字符串,用作其屬性(鍵)。

1、filter()

舉例:

我們想要得到這個列表中年齡小于或等于24歲的所有學生。我們需要使用filter方法來過濾掉所有大于 24 歲的學生。

輸出:
const students = [
 {name: "sam", age: 26, score: 80},
 {name: "john", age: 25, score: 86},
 {name: "dean", age: 20, score: 78},
 {name: "timothy", age: 18, score: 95},
 {name: "frank", age: 21, score: 67}
]
const filteredStudents = students.filter((students) => {
//this filter method just takes a single function, which is going to have one parameter of   student   which is every student/item in the array
// we'd need to return a true or false statement on whether or not we want to include them in the   new array
return students.age <= 24
//This line basically stashes all of the students whose ages are less than a 24 in the new   filteredStudents array
})
console.log(filteredStudents)
//
所做的就是為每個項目返回 true 或 false。如果為真,則將其添加到新數組中,如果為假,則不添加。它們實際上不會更改你正在過濾的底層對象,因此可以記錄學生數組,并且它仍然包含所有項目。在使用這些新數組方法創建新數組時,不必擔心更改舊數組。

2、map()
舉例:

map()允許你獲取一個數組并將其轉換為一個新數組。在這個中,新數組中的所有項目看起來都略有不同。如果我們想得到每個學生的名字。我們可以通過使用 map() 獲取名稱數組。

輸出:

const students = [
 {name: "sam", age: 26, score: 80},
 {name: "john", age: 25, score: 86},
 {name: "dean", age: 20, score: 78},
 {name: "timothy", age: 18, score: 95},
 {name: "frank", age: 21, score: 67}
]
const studentNames = students.map((students) => {
//the method takes a single function, with the students/items as a parameter
// here we just return what we want in the new array
return students.name
})
console.log(studentNames)
/*If we print out these student names,
console.log(studentNames)
we get a new array that is just full of all the different names of the students.*/
/******************/
這可以通過數組中的任何內容來完成。例如,當你想要獲取一個對象,并且只獲取名稱或單個鍵,或者獲取一個數組并將其轉換為另一個數組時,該方法非常有用。此方法是 JavaScript 數組中最流行的方法之一。

3、find()
此方法允許你在數組中查找單個對象。這是直截了當的方法。

假設我們想找到一個名叫 john 的學生。
const students = [
 {name: "sam", age: 26, score: 80},
 {name: "john", age: 25, score: 86},
 {name: "dean", age: 20, score: 78},
 {name: "timothy", age: 18, score: 95},
 {name: "frank", age: 21, score: 67}
]
const singleStudent = students.find((students) => {
return students.name === 'john'
})
console.log(singleStudent)
/*
 console.log(singleStudent) will give the everything associated with john in this example, I.e
  Object {
   age: 25,
   name: "john",
   score: 86
 }
*/
在這個方法中,邏輯是有一個 true 或 false 語句,它將返回第一個計算結果為 true 的數組項。
4、forEach()
與其他方法不同, forEach() 實際上不返回任何內容,因此不需要 return 語句。假設我們需要打印出 student 數組中所有學生的名字,可以這樣做:
const students = [
 {name: "sam", age: 26, score: 80},
 {name: "john", age: 25, score: 86},
 {name: "dean", age: 20, score: 78},
 {name: "timothy", age: 18, score: 95},
 {name: "frank", age: 21, score: 67}
]
students.forEach((students) => {
console.log(students.name)
})
/*
 the result is the name property printed out on the console line after line like so
 "sam"
 "john"
 "dean"
 "timothy"
 "frank"
*/
這將與 for 循環非常相似,但它將采用一個函數。因此,對于每個數組項,它將執行函數內的代碼塊。
這種方法只是使處理需要循環項目的數組變得更加容易,因為你不必像通常那樣寫出笨重而長的for循環語句。

5、some()
這個方法與我們的大多數其他函數有點不同,它不是返回一個全新的數組,而是返回 true 或 false。所以我們可以檢查這個數組中是否有一些學生是未成年人。
const students = [
 {name: "sam", age: 26, score: 80},
 {name: "john", age: 25, score: 86},
 {name: "dean", age: 20, score: 78},
 {name: "timothy", age: 18, score: 95},
 {name: "frank", age: 21, score: 67}
]
const hasUnderageStudents = students.some((students) => {
return students.age < 18
})
console.log(hasUnderageStudents)
/*
 this will return false because there is no underage student in the array.
*/
因此,如果任何學生項目的年齡值低于 18,該函數將返回 true。該方法只是檢查數組中是否有任何內容返回 true,如果是,則整個內容返回 true。
6、every()
此方法與 some() 非常相似,除了檢查至少一項以評估為真或假之外,它會檢查以確保每一項都符合給定的條件。
如果我使用 some() 代碼示例中使用的相同代碼。
const students = [
 {name: "sam", age: 26, score: 80},
 {name: "john", age: 25, score: 86},
 {name: "dean", age: 20, score: 78},
 {name: "timothy", age: 18, score: 95},
 {name: "frank", age: 21, score: 67}
]
const hasUnderageStudents = students.every((students) => {
return students.age < 18
})
console.log(hasUnderageStudents)
// this returns false because there are no ages below 18
如果我要將條件更改為每個項目評估為真的條件,則整個事情都會返回真。
const students = [
 {name: "sam", age: 26, score: 80},
 {name: "john", age: 25, score: 86},
 {name: "dean", age: 20, score: 78},
 {name: "timothy", age: 18, score: 95},
 {name: "frank", age: 21, score: 67}
]
const hasUnderageStudents = students.every((students) => {
return students.age < 40
})
console.log(hasUnderageStudents)
// this will return true because every single age property is below 40

7、reduce()
此方法與所有其他方法完全不同,因為它實際上是對數組進行一些操作并返回所有這些不同操作的組合。
假設我想要學生數組中所有項目的總分,通常會進行 for 循環并每次都將分數相加,在循環結束時,將打印出總分。為了避免冗長的過程,你可以使用 reduce() 方法來執行此操作。
簡化方法的語法與其他方法不同。它不是采用數組本身,而是采用一個數組和一個屬性,用于我們想要將所有內容減少到和在我們的示例中的分數。除了函數之外,它還需要第二個參數,這將是你的起點,在我們的例子中,我們希望從零開始我們的總數。
const students = [
 {name: "sam", age: 26, score: 80},
 {name: "john", age: 25, score: 86},
 {name: "dean", age: 20, score: 78},
 {name: "timothy", age: 18, score: 95},
 {name: "frank", age: 21, score: 67}
]
const totalScore = students.reduce((currentTotal, students) => {
return students.score + currentTotal
}, 0)
console.log(totalScore)
在代碼讀取時,我們有 reduce() 方法,該方法對數組中的每一項運行一個函數。
該函數的第一個參數將是該數組的前一次迭代返回的任何內容,第二個參數是數組中的實際項目。
currentTotal 將在第一次迭代時開始,使用我們作為 reduce() 方法的第二個參數傳入的任何內容,在我們的例子中為零。
這個 reduce() 方法第一次運行,所以我們得到零和“sam”項。所以它只是做了80 加零并返回那個值,也就是 80。
該方法第二次運行時,返回值 80 成為當前總數,我們的下一項是 'john',所以它計算了 86 加上我們當前的總數 80,即 166,并將其放回當前總數。它一直這樣做,直到我們到達數組中的最后一個項目,該項目將作為 totalScore 變量中的總數輸出。
這種方法有點混亂,但是,當你需要對數組中的所有項目進行某種累積操作時,它非常有用,例如,獲取所有項目的總價,每日股票價格等。

8、includes()
此方法不接受函數作為參數。它需要一個參數。它通常用于不需要大量繁重操作的簡單數組中。假設你有一堆水果,
const Fruits = [ 'mango', 'apple', 'orange', 'pineapple', 'lemon']
contains() 方法實現的是一種簡單方法,是檢查某個水果是否在數組中。
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.

以上就是小編今天的分享了,希望可以幫助到大家。

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

js數組常用方法

數組是一種特殊的變量,它能夠一次存放一個以上的值。js里的數組和其他語言中的數組是不同的,實際它并不是數組,而是一種array-like特性的對象。常用數組:1、filter;2、map;3、find;4、forEach;5、some;6、every;7、reduce;8、includes。
推薦度:
標簽: js數組
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 亚洲欧美一区二区三区国产精品 | 国产精品久久新婚兰兰 | 精品久久久久久国产牛牛app | 图片亚洲va欧美va国产综合 | 免费a黄色 | 久久久久亚洲精品成人网小说 | 国产精品亚洲四区在线观看 | 欧美影欧美影院免费观看视频 | 久艹网| 国产精品成久久久久三级 | 日韩高清dvd| 国产成人精品一区二区三在线观看 | 特黄特黄aaaa级毛片免费看 | 欧美亚洲激情 | 日韩精品欧美高清区 | 国产日韩在线观看视频网站 | 毛片一级免费 | 欧美a区| 免费在线国产视频 | 伊人操 | 国产一区二区三区视频 | 中文字幕美日韩在线高清 | 国产网站视频 | 国产91久久精品一区二区 | 亚洲欧洲日本在线观看 | 国产自产 | 欧美不卡一区二区三区 | 亚洲 欧美 中文 日韩专区 | 国产三级一区二区 | 91精品久久久久久久久久 | 一级a毛片免费观看久久精品 | 夜精品a一区二区三区 | 精品91精品91精品国产片 | 欧美一级在线 | 欧美精品一二三 | 色综合色狠狠天天综合色 | 久久精品无码一区二区日韩av | 久久精品无遮挡一级毛片 | 欧美亚洲国产精品第一页 | 97精品国产97久久久久久 | 人人爽天天碰天天躁夜夜躁 |