简体   繁体   English

如何在JavaScript中遍历此数组?

[英]How can I loop through this array in javascript?

I'm trying to loop through this array but it does not return any length. 我试图遍历此数组,但它不返回任何长度。 Is it possible? 可能吗?

Thanks! 谢谢!

<!DOCTYPE html>
<html>
  <body>
    <button onclick="myFunction()">click</button>
    <script>
      function myFunction()
      {
        var fruits = [];
        fruits["B"] = "Banana";
        fruits["A"] = "Apple";
        fruits["O"] = "Orange";

        delete fruits["A"];

        alert(fruits["A"]); // Alerts "undefined"
        alert(fruits["B"]); // Alerts "Banana"
        alert(fruits.length); // Alerts "0"
      }
    </script>

  </body>
</html>

Arrays can only have numeric indexes. 数组只能有数字索引。 When you write fruits["B"] = "Banana"; 当您写fruits["B"] = "Banana"; you're assigning a property to the object (Arrays are objects), not adding an item to the array. 您正在为对象分配属性(数组是对象),而不是向数组添加项目。

You can either use an array properly with numeric indexes, or use an object with string keys instead: 您可以使用带有数字索引的数组,也可以使用带有字符串键的对象:

var fruits = [];
fruits.push("Banana");
alert(fruits[0]); // "Banana"

var fruits = {};
fruits["B"] = "Banana";
alert(fruits["B"]); // "Banana"

If you need the length of the object, use Object.keys(fruits).length; 如果需要对象的长度,请使用Object.keys(fruits).length; Docs 文件

Looks like you want an object instead of an array so you can access an element by key: 看起来您想要一个对象而不是数组,因此可以通过键访问元素:

var fruits = {
    a : 'apple',
    b : 'banana'
};

fruits.a //apple
fruits['b'] //banana

then you can loop through them by key: 那么您可以通过按键遍历它们:

for( var item in fruits ){
    fruits[ item ];
}

One thing to note is that this will loop through all keys in fruits AND any object it inherits from (in my example its only object.prototype). 需要注意的一件事是,这将循环遍历水果中的所有键及其继承的任何对象(在我的示例中,这是唯一的object.prototype)。 If you wanted to be careful you could use the hasOwnProperty method to make sure that the property exists on the fruits object and not any of its prototypes: 如果要小心,可以使用hasOwnProperty方法来确保该属性存在于fruits对象上,而不是其任何原型上:

for( var item in fruits ){

    if( fruits.hasOwnProperty( item ) ){
        fruits[ item ];
    }
}

What you are looking for is a JavaScript object. 您正在寻找的是一个JavaScript对象。 If you declare your fruits variable as an object literal, you can perform the actions that you desire. 如果将水果变量声明为对象文字,则可以执行所需的操作。

var fruits = {};
fruits["B"] = "Banana"; // Can also do this as fruits.B = "Banana";
fruits["A"] = "Apple";
etc.

Then doing things like alert(fruits["A"]) will give you an alert message that says "Apple" as you desire. 然后,执行alert(fruits["A"])会给您一条警告消息,您可以根据需要说“ Apple”。 However, JavaScript objects do not have a length property, but you can do Object.keys(fruits) which will return an array of the keys of your fruits variable, and then you can access the length property of that. 但是,JavaScript对象没有length属性,但是您可以执行Object.keys(fruits) ,该操作将返回fruit变量的键的数组,然后可以访问该length属性。

You can not declare array like this.you need to declare array using new keyword 您不能像这样声明数组。您需要使用new关键字声明数组

var Fruit=new Array("Banana","Apple","Orange");

then you can access like this 那么你可以像这样访问

 alert(Fruit[0]);//Banana
    alert(Fruit[1]);//Apple
    alert(Fruit[2]);/Orange

Hope this will work 希望这会起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM