Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.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';对于in';函数,只返回一个索引?_Javascript - Fatal编程技术网

Javascript';对于in';函数,只返回一个索引?

Javascript';对于in';函数,只返回一个索引?,javascript,Javascript,这是我尝试运行的方法: function SayHello() { cars = new Array(); cars[0] = "Toyota"; cars[1] = "Mitsubishi"; cars[2] = "Honda"; for (car in cars) { alert(car); } } 这返回: 0 1 2 当我将代码更改为以下内容时: function SayHello() { cars = new

这是我尝试运行的方法:

function SayHello() {
    cars = new Array();
    cars[0] = "Toyota";
    cars[1] = "Mitsubishi";
    cars[2] = "Honda";

    for (car in cars) {
        alert(car);
    }
}
这返回:

0
1
2
当我将代码更改为以下内容时:

function SayHello() {
    cars = new Array();
    cars[0] = "Toyota";
    cars[1] = "Mitsubishi";
    cars[2] = "Honda";

    for (car in cars) {
        alert(cars[car]);
    }
}
它正确地返回了名称


我的问题是,for-in循环是否只是以有序的方式返回索引?谢谢。

是的,它将成为收藏中的索引

见:

var mycars=[“萨博”、“沃尔沃”、“宝马”];
用于(mycars中的var car)
{
文件。写入(mycars[car]+“
”; }
如您所见,使用变量作为集合的索引

您可以对每个。。。在语法(在Javascript 1.6中引入)中,将对值进行迭代。看

对于每个…in
-类似于
对于…in
,但迭代对象属性的值,而不是属性名称本身。(JavaScript1.6中新增。)


据我所知,Javascript 1.6+目前只在Firefox中使用。

是和否。它返回索引,而不是值,并将它们作为带引号的字符串返回。“0”、“1”等

这样做的好处是,如果将javascript对象用作关联数组,则in中的
的工作原理相同

它返回每个项目的“键”。使用这样的“阵列”也将获得相同的结果:


是的,迭代器的值是属性的名称。然而,使用它在阵列上循环是非常不受欢迎的。例如,考虑这一点:

x = ['a', 'b', 'c'];

x.foo = 'bar';

for (i in x) alert(i);  // 0, 1, 2, foo
它用于迭代对象的成员:

x = { a : 'apple', b : 'banana', c : 'carrot' };

for (i in x) {
    // and it's best to check that the property actually exists
    // on this object, not just on one of its prototypal ancestors:
    if (x.hasOwnProperty(i)) {
        alert(i);  // 'a', 'b', 'c'
    }
}
更多关于为什么在

上使用常规for

for (var i = 0; i < cars.length; i++) {
   let car = cars[i];
   // Do what you want from here
}
for(变量i=0;i
-迭代由字符串设置关键字的对象的所有可枚举属性(对于简单数组,它返回元素的索引)


-迭代属性值,但只能用于可迭代类型:内置字符串、数组、类似数组的对象(例如参数或节点列表)、TypedArray、Map、Set和用户定义的iterables(不适用于泛型对象)。

回答错误。。。1. <在JS2中,顶部的code>var x
是愚蠢的。使用
[“萨博”、“沃尔沃”、“宝马”]
创建数组时,应避免使用
数组
构造函数(看看需要哪些参数)3。在
@Oded中,
中通常缺少的
hasOwnProperty
,这只会进一步破坏这个答案的可信度-W3School何时成为学习JavaScript的主要权威?@Oded哦,对不起,错过了第4点。链接到w3schools。光是这一点就值得这里的每个人投反对票:/@Ivo Wetzel——以w3schools的逐字记录为例。你能解释一下关于
hasOwnProperty
的第三点是什么意思,或者添加一个链接到一个好的解释吗?@Oded
for…in
还列出了原型链上的内容
Object.prototype.foo=2
,现在foo将按
中的每个
列出
hasOwnProperty
将检查该属性是否在对象上实际定义。请参阅:简单的答案是:返回您正在迭代的对象的属性的名称。在本例中,当您说cars[0]=而不是cars.push(…)时,您正在创建一个名为0的属性。要遍历这些值,您需要一个for(var i=0;iisn不是javascript吗?:D
x = { a : 'apple', b : 'banana', c : 'carrot' };

for (i in x) {
    // and it's best to check that the property actually exists
    // on this object, not just on one of its prototypal ancestors:
    if (x.hasOwnProperty(i)) {
        alert(i);  // 'a', 'b', 'c'
    }
}
for (var i = 0; i < cars.length; i++) {
   let car = cars[i];
   // Do what you want from here
}