Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从数组的长度中减去_Javascript_String Length - Fatal编程技术网

Javascript 从数组的长度中减去

Javascript 从数组的长度中减去,javascript,string-length,Javascript,String Length,需要澄清以下内容。答案是“狗”,想知道为什么 例如: var animals = ["parrot", "cat", "dog"]; console.log(animals[animals.length - 1]); Console=“狗” 动物长度=3,因为包含3种元素。 动物。长度-1=第二个元素(3-1)。雷特 如果在索引2中的Animal中看到,该值是dog,因为数组从索引0开始 Animal[0] = parrot Animal[1]=cat Animal[2]=dog

需要澄清以下内容。答案是“狗”,想知道为什么

例如:

var animals = ["parrot", "cat", "dog"];

console.log(animals[animals.length - 1]);

Console=“狗”

动物长度=3,因为包含3种元素。
动物。长度-1=第二个元素(3-1)。雷特

如果在索引2中的Animal中看到,该值是dog,因为数组从索引0开始

 Animal[0] = parrot
 Animal[1]=cat 
 Animal[2]=dog

array[index]
是一种访问数组元素的方法。在您的情况下,数组中的
animals
animal.length-1
将是一个数字。
index
从0开始,因此
animal.length-1
将指向索引为2的元素。在这个数组中,
dog
占据第二个索引,因此它是安慰dog

var动物=[“鹦鹉”、“猫”、“狗”];
console.log(anists.length)//3
var_指数=动物。长度-1//2

log(animates[\u index])//将打印javascript中索引2中的元素
数组的索引为0。因此,最后一个元素位于索引
length-1
,正如第一个元素位于
1-1
i-th
元素的索引位于
i-1
。数组的长度为3,索引为0、1和2。