整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

七爪源码:如何使用 JavaScript 查找数组中

七爪源码:如何使用 JavaScript 查找数组中的奇数

本文中,我们将研究使用 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。

置相关的结构伪类

:first-child 代表了一组兄弟元素中的第一个元素

:last-child 代表父元素的最后一个子元素。

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

: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类型的孩子,而且也只对这一类型进行计数。不是这一类型的孩子直接被忽略掉