本文中,我們將研究使用 JavaScript 在數組中查找奇數的不同方法。
1. 數組 filter() 方法
要查找數組中的奇數,我們可以調用 Array filter() 方法,傳遞一個回調,當數字為奇數時返回 true,否則返回 false。
const numbers=[8, 19, 5, 6, 14, 9, 13];const odds=numbers.filter((num)=> num % 2===1);
console.log(odds); // [19, 5 , 9, 13]
filter() 方法創建一個新數組,其中包含通過測試回調函數中指定的測試的所有元素。 因此,filter() 返回原始數組中所有奇數的數組。
2. 數組 forEach() 方法
在 JavaScript 數組中查找奇數的另一種方法是使用 Array forEach() 方法。 我們在數組上調用 forEach(),在回調中,我們只在結果數組中添加一個元素,如果它是奇數。 例如:
const numbers=[8, 19, 5, 6, 14, 9, 13];const odds=[];
numbers.forEach((num)=> {
if (num % 2===1) {
odds.push(num);
}
});console.log(odds); // [19, 5, 9, 13]
3. for...of 循環
我們可以使用 for...of 循環代替 forEach() 來遍歷數組:
const numbers=[8, 19, 5, 6, 14, 9, 13];const odds=[];
for (const num of numbers) {
if (num % 2===1) {
odds.push(num);
}
}
console.log(odds); // [19, 5, 9, 13]
關注七爪網,獲取更多APP/小程序/網站源碼資源!
HTML 中,正確的字符編碼是什么?
HTML5 中默認的字符編碼是 UTF-8。
這并非總是如此。早期網絡的字符編碼是 ASCII 碼。
后來,從 HTML 2.0 到 HTML 4.01,ISO-8859-1 被認定為標準。
隨著 XML 和 HTML5 的出現,UTF-8 也終于到來了,解決了大量的字符編碼問題。
下面是關于字符編碼標準的簡短概述。
在開始的時候:ASCII
計算機信息(數字、文字、圖片)在電子中是以二進制 1 和 0(01000101)進行存儲的。
為了規范字母數字字符的存儲,創建了 ASCII(全稱 American Standard Code for Information Interchange)。它為每個存儲字符定義了一個獨特的二元 7 位數字,支持 0-9 數字,大/小寫英文字母(a-z、A-Z)和一些特殊的字符,比如 ! $ + - ( ) @ < > 。
由于 ASCII 使用一個字節(7 位表示字符,1 位表示傳輸奇偶控制),所以它只能表示 128 個不同的字符。這些字符中有 32 個被保留作為其他控制目的使用。
ASCII 的最大的缺點是,它排除了非英文字母。
ASCII 今天仍然在廣泛使用,尤其是在大型計算機系統中。
如需深入了解 ASCII,請查看完整的 ASCII 參考手冊。
在 Windows 中:ANSI
ANSI(也稱為 Windows-1252),是 Windows 95 及其之前的 Windows 系統中默認的字符集。
ANSI 是 ASCII 的擴展,它加入了國際字符。它使用一個完整的字節(8 位)來表示 256 個不同字符。
自從 ANSI 成為 Windows 中默認的字符集,所有的瀏覽器都支持 ANSI。
如需深入了解 ANSI,請查看完整的 ANSI 參考手冊。
在 HTML 4 中:ISO-8859-1
由于大多數國家使用 ASCII 以外的字符,在 HTML 2.0 標準中,默認的字符編碼更改為 ISO-8859-1。
ISO-8859-1 是 ASCII 的擴展,它加入了國際字符。與 ANSI 一樣,它使用一個完整的字節(8 位)來表示 256 個不同字符。
如果 HTML 4 網頁使用了不同于 ISO-8859-1 的字符集,則需要在 <meta> 標簽中指定,如下所示:
實例
<metahttp-equiv="Content-Type"content="text/html;charset=ISO-8859-8">
如需深入了解 ISO-8859-1,請查看完整的 ISO-8859-1 參考手冊。
在 HTML5 中:Unicode(UTF-8)
由于以上所列的字符集是有限的,在多語言環境中是不兼容的,所以 Unicode 聯盟(Unicode Consortium)開發了 Unicode 標準(Unicode Standard)。
Unicode 標準覆蓋了(幾乎)所有的字符、標點符號和符號。
Unicode 使文本的處理、存儲和運輸,獨立于平臺和語言。
HTML5 中默認的字符編碼是 UTF-8。
如您還有不明白的可以在下面與我留言或是與我探討QQ群308855039,我們一起飛!
當瀏覽器在網頁中檢測到 ISO-8859-1 時,通常默認為 ANSI,因為除了 ANSI 有 32 個額外的字符這一點,其他方面 ANSI 基本等同于 ISO-8859-1。
HTML5 中默認的字符集是 UTF-8。
所有的 HTML 4 處理器都支持 UTF-8,所有的 HTML5 和 XML 處理器都支持 UTF-8 和 UTF-16。
span:first-child{ background:lime; } li{ color:red; } li:first-child,li:last-child{ color:green; } //////////////////////////////////////////////////////////////////////////////// <span>This span is limed</span> <span>This span is not :first-child</span> <ul> <li>List 1</li> <li>List 2</li> <li>List 3</li> <li>List 4</li> <li>List 5</li> <li>List 6</li> </ul> |
:nth-child(an+b) 這個 匹配文檔樹中在其之前具有 an+b-1 個兄弟節點的元素,其中 n 為正值或零值。簡單點說就是,這個選擇器匹配那些在同系列兄弟節點中的位置與模式 an+b 匹配的元素。
示例:
tr:nth-child(2n+1)
表示HTML表格中的奇數行。
tr:nth-child(odd)
表示HTML表格中的奇數行。
tr:nth-child(2n)
表示HTML表格中的偶數行。
tr:nth-child(even)
表示HTML表格中的偶數行。
span:nth-child(0n+1)
表示子元素中第一個且為span的元素,與 選擇器作用相同。
span:nth-child(1)
表示父元素中子元素為第一的并且名字為span的標簽被選中
span:nth-child(-n+3)
匹配前三個子元素中的span元素。
示例
.first span:nth-child(2n+1), .second span:nth-child(2n+1), .third span:nth-of-type(2n+1) { background-color: lime; } <div> <a href="#">THIS IS a </a> <span>This span is selected!</span> <span>This span is not. :(</span> <span>What about this?</span> <span>And this one?</span> <span>Another example</span> <span>Yet another example</span> <span>Aaaaand another</span> </div> |
3, 5, and 7 are selected, 1沒有被選中,因為1不是span,但被計數
<div> <span>This span is selected!</span> <span>This span is not. :(</span> <em>This one is an em.</em> <span>What about this?</span> <span>And this one?</span> <span>Another example</span> <span>Yet another example</span> <span>Aaaaand another</span> </div> |
Children 1, 5, and 7 are selected.
3因為也是孩子,所以被計數,但沒有被選中,因為不是span。
<div> <span>This span is selected!</span> <span>This span is not. :(</span> <em>This one is an em.</em> <span>What about this?</span> <span>And this one?</span> <span>Another example</span> <span>Yet another example</span> <span>Aaaaand another</span> </div> .third span:nth-of-type(2n+1) { background-color: lime; } |
Children 1, 4, 6, and 8 are selected.
3 沒有被計數,因為用的CSS是span:nth-of-type 只選擇span類型的孩子,而且也只對這一類型進行計數。不是這一類型的孩子直接被忽略掉
*請認真填寫需求信息,我們會在24小時內與您取得聯系。