成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

五個必須知道的 JavaScript 數(shù)組方法,讓你的生活更輕松

開發(fā) 前端
數(shù)組非常適合存儲相關(guān)數(shù)據(jù),并且通常用作組織信息的一種方式。 我們中的大多數(shù)人每天都在使用它們,但是您知道 JavaScript 中還內(nèi)置了一些非常簡潔的數(shù)組方法嗎?

介紹

數(shù)組非常適合存儲相關(guān)數(shù)據(jù),并且通常用作組織信息的一種方式。 我們中的大多數(shù)人每天都在使用它們,但是您知道 JavaScript 中還內(nèi)置了一些非常簡潔的數(shù)組方法嗎?

這些方法使我們的生活變得更加輕松,將多行代碼優(yōu)化為一個簡單的命令。 無論您是剛開始使用數(shù)組還是已經(jīng)感覺自己是專家,本文都將幫助您在使用它們時變得更有效率。

filter()

如果您想根據(jù)特定條件過濾數(shù)組,您可能需要 filter() 方法。 這是一個有用的函數(shù),它將返回一個包含您感興趣的所有項目的新數(shù)組。

它需要一個函數(shù)作為參數(shù),它將為數(shù)組中的每個元素調(diào)用。 如果函數(shù)返回 true,則該元素將保留在數(shù)組中; 否則,它將從數(shù)組中刪除。

例子

我們已從后端請求數(shù)據(jù),并希望根據(jù)對象數(shù)組具有的屬性進(jìn)行客戶端過濾。 在這種情況下,我們已從 JokeAPI 請求笑話,并希望過濾類別屬性等于 Programming 的笑話。

const response = {
"error": false,
"amount": 4,
"jokes": [
{
"category": "Programming",
"type": "single",
"joke": "Judge: \"I sentence you to the maximum punishment...\"\nMe (thinking): \"Please be death, please be death...\"\nJudge: \"Learn Java!\"\nMe: \"Damn.\"",
"id": 45,
"safe": true,
"lang": "en"
},
{
"category": "Christmas",
"type": "twopart",
"setup": "How will Christmas dinner be different after Brexit?",
"delivery": "No Brussels!",
"id": 251,
"safe": false,
"lang": "en"
},
{
"category": "Christmas",
"type": "twopart",
"setup": "What do Santa's little helpers learn at school?",
"delivery": "The elf-abet!\n",
"id": 248,
"safe": true,
"lang": "en"
},
{
"category": "Christmas",
"type": "twopart",
"setup": "Why couldn't the skeleton go to the Christmas party?",
"delivery": "Because he had no body to go with!",
"id": 252,
"safe": true,
"lang": "en"
}
]
}

const programmingJokes = response.jokes.filter((joke) =>
joke.category === "Programming"
);

console.log("programmingJokes: ", programmingJokes);
programmingJokes: [
{
"category": "Programming",
"type": "single",
"joke": "Judge: \"I sentence you to the maximum punishment...\"\nMe (thinking): \"Please be death, please be death...\"\nJudge: \"Learn Java!\"\nMe: \"Damn.\"",
"id": 45,
"safe": true,
"lang": "en"
},
]

map()

map() 方法轉(zhuǎn)換數(shù)組中的每一項,對其應(yīng)用一個函數(shù)并將結(jié)果存儲在一個新數(shù)組中,而不實際更改初始數(shù)組。

例子

我們已從后端請求數(shù)據(jù),并希望從該數(shù)據(jù)中提取信息。 在這種情況下,我們從 RandomDataAPI 請求隨機(jī)用戶數(shù)據(jù),并希望將每個人的年齡提取到一個數(shù)組中。

const response = [
{
"id": 7433,
"uid": "4c2c1731-2c3c-4983-b39f-0f988791e98f",
"password": "L903JpXGAj",
"first_name": "Dalene",
"last_name": "Kuhn",
"username": "dalene.kuhn",
"email": "dalene.kuhn@email.com",
"avatar": "https://robohash.org/autmagnisunt.png?size=300x300&set=set1",
"gender": "Agender",
"phone_number": "+964 771-857-9446 x77784",
"social_insurance_number": "607847845",
"age": 25,
},
{
"id": 3764,
"uid": "0c1c9485-2b90-4e68-a795-0e4925aa8344",
"password": "XjyI92Y1dl",
"first_name": "Laurence",
"last_name": "Lowe",
"username": "laurence.lowe",
"email": "laurence.lowe@email.com",
"avatar": "https://robohash.org/quinonomnis.png?size=300x300&set=set1",
"gender": "Agender",
"phone_number": "+689 743-128-5476 x530",
"social_insurance_number": "737935460",
"age": 30,
},
{
"id": 9408,
"uid": "4933cb5d-f4f5-4bc3-8d37-f4c9b3129923",
"password": "JrI8e4KVjs",
"first_name": "Gabriella",
"last_name": "Tillman",
"username": "gabriella.tillman",
"email": "gabriella.tillman@email.com",
"avatar": "https://robohash.org/repellatmaioresmolestiae.png?size=300x300&set=set1",
"gender": "Bigender",
"phone_number": "+675 552-834-4168 x39534",
"age": 21,
}
]

const arrayOfAges = response.map(person person.age);
console.log("arrayOfAges: ", arrayOfAges)
arrayOfAges: [25, 30, 21]

reduce()

reduce() 方法通過對每個元素應(yīng)用一個函數(shù)并累積結(jié)果,將數(shù)組縮減為單個值。 這是查找總數(shù)或查找所有項目平均值的好方法。

例子

我們有一個包含每月存款的數(shù)組,我們想知道所有存款的總和。

const depositsArray = [{
id: 1231,
deposit: 5,
currency: '$',},{
id: 1231,
deposit: 10,
currency: '$',},{
id: 1231,
deposit: 20,
currency: '$',},{
id: 1231,
deposit: 5,
currency: '$',},{
id: 1231,
deposit: 15,
currency: '$',},
];



const sumOfDeposits = depositsArray.reduce((total, transaction) =>
total + transaction.deposit, 0
);

console.log('depositsArray: ', depositsArray);
console.log('sumOfDeposits: ', sumOfDeposits);
depositsArray: [{...}, {...}, {...}, {...}, {...}]
sumOfDeposits: 55

some()

some() 方法檢查數(shù)組中的至少一個元素是否滿足由提供的函數(shù)實現(xiàn)的測試。 如果它確實滿足測試,它將返回true; 否則,它將返回 false。

例子

我們已從后端請求用戶,并想知道其中一個是否已被標(biāo)記為機(jī)器人。

const response = [
{
id: 101,
firstName: 'Muhammad',
lastName: 'Ovi',
age: 25,
isBot: false,
},
{
id: 102,
firstName: 'John',
lastName: 'Doe',
age: 30,
isBot: true,
},
{
id: 103,
firstName: 'Chris',
lastName: 'Smith',
age: 27,
isBot: false,
},
];

const isNotValidUsers = response.some((user) => user.isBot === false);

console.log("isNotValidUsers: ", isNotValidUsers)
isNotValidUsers: true

every()

every() 方法檢查數(shù)組中的每個元素是否滿足由提供的函數(shù)實現(xiàn)的測試。 如果是,它將返回 true; 否則,它將返回 false

例子

我們的購物車中有一份產(chǎn)品清單,想檢查是否有庫存。

const response = [
{
"id": 1,
"title": "iPhone 9",
"price": 549,
"discountPercentage": 12.96,
"rating": 4.69,
"stock": 94
},
{
"id": 2,
"title": "Apple Watch",
"price": 300,
"discountPercentage": 10,
"rating": 4.40,
"stock": 20
},
{
"id": 3,
"title": "Apple Headphones",
"price": 600,
"discountPercentage": 7,
"rating": 4.65,
"stock": 2
},
]

const hasStock = response.every((item) => item.stock > 0);

console.log("hasStock: ", hasStock);
hasStock: true

結(jié)論

數(shù)組是任何編程語言中最基本和最重要的數(shù)據(jù)結(jié)構(gòu)之一。 在學(xué)習(xí) JavaScript 時,了解如何使用這些數(shù)組方法更有效地操作和存儲數(shù)據(jù)會很有幫助。 這些方法包括 filter()、map()、reduce()、some() 和 every(),它們可以幫助您提高代碼效率。

責(zé)任編輯:華軒 來源: 七爪網(wǎng)
相關(guān)推薦

2020-03-19 15:30:08

JavaScript數(shù)組字符串

2022-09-27 14:36:57

JavaScrip數(shù)組開發(fā)

2022-04-28 08:41:53

JavaScript數(shù)組

2023-07-04 15:52:49

JavaScript數(shù)組

2023-06-02 15:53:38

工具Python開發(fā)

2024-10-11 13:17:16

Linux命令行快捷導(dǎo)航

2023-06-29 15:08:21

JavaScrip開發(fā)

2022-11-07 16:25:07

JavaScript技巧

2015-07-23 10:37:13

Linux命令

2023-02-06 16:46:59

JavaScript程序員技巧

2022-08-10 12:02:52

面試JavaScript

2011-05-11 15:28:05

2020-07-20 08:40:42

轉(zhuǎn)型

2022-11-13 15:33:30

JavaScript數(shù)組開發(fā)

2024-09-18 15:58:05

2017-12-07 15:47:25

2020-02-28 14:05:00

Linuxshell命令

2012-09-29 09:22:24

.NETGC內(nèi)存分配

2012-09-29 10:29:56

.Net內(nèi)存分配繼承

2017-12-07 15:28:36

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 亚州一区二区三区 | 精品亚洲一区二区三区 | 99热碰| 亚洲欧美国产毛片在线 | 欧美黄在线观看 | 一区二区三区高清 | 欧美在线日韩 | 亚洲 欧美 激情 另类 校园 | 影音先锋亚洲资源 | 不卡一区 | 一区二区三区四区国产 | 国产精品乱码一区二三区小蝌蚪 | 美女一区| 日韩在线视频免费观看 | 国产精品123区 | 久久久久久中文字幕 | 国产精品国产a级 | 久久亚洲一区二区三区四区 | 久久国产精品一区二区 | 91免费观看在线 | 欧美成年黄网站色视频 | 成人免费看黄网站在线观看 | 欧美一级黄色片 | 成年人免费在线视频 | 精品国产一区二区三区成人影院 | 91精品国产91 | 九九九久久国产免费 | 五月婷婷丁香婷婷 | 九九精品在线 | 久久久免费电影 | 国产精品久久一区 | 亚洲另类自拍 | www.五月天婷婷.com | 日韩毛片在线观看 | 韩国欧洲一级毛片 | 黄色高清视频 | 日本精品久久久一区二区三区 | 精品一区二区三区在线观看 | 激情在线视频网站 | 欧美精品一区二区在线观看 | 精品国产一区久久 |