简体   繁体   English

如何获得正确的数组长度

[英]How to get the correct array length

In my code i initialize array then put a value inside it why the output be 0 ?in spite of this should be 1在我的代码中,我初始化数组然后在其中放入一个值为什么输出为 0 ?尽管这应该是 1

       var changedfields=[];
       changedfields['product']="productname";
       alert(changedfields.length);

You're creating an associative array (normal arrays have an numeric index) but are actually trying to build a HashMap (a key, value pair).您正在创建一个关联数组(普通数组有一个数字索引),但实际上是在尝试构建一个 HashMap(一个键值对)。 Use Objects or ES6 Maps .使用对象或ES6 地图

I hope the following example will help you out:我希望下面的例子能帮助你:

var changedfields = {}; // create an object
changedfields['product']="productname";
var keys = Object.keys(changedfields);  // returns the keys of the object ['product']
alert(keys.length);

I would suggest to read more about datastructures in javascript and avoid associative arrays in general.我建议阅读有关 javascript 中数据结构的更多信息,并一般避免使用关联数组。

Your question is interesting.你的问题很有趣。 Following is the answer.以下是答案。

First of all Arrays in Javascript are object type.首先,Javascript 中的数组是对象类型。

The first line you wrote creates an empty array and it's type is object.你写的第一行创建了一个空数组,它的类型是对象。

var changedfields=[];

The second line you wrote creates a property called product of changedfields and sets the value to productname .您编写的第二行创建了一个名为product of changedfields的属性,并将值设置为productname It allows you add the product property because Javascript Array type is object.它允许您添加产品属性,因为 Javascript 数组类型是对象。 If you just had var changedfields;如果你只有var changedfields; you could not add this property.您无法添加此属性。

changedfields['product']="productname";

The third line you wrote simply finds the length of the empty array.您编写的第三行只是查找空数组的长度。

alert(changedfields.length);

In Javascript associative arrays are achieved using object.在 Javascript 中,关联数组是使用 object 实现的。 But if you want to add the product name in changedfields array.但是如果你想在changedfields数组中添加产品名称。 You could use push method like below and check the length:您可以使用如下推送方法并检查长度:

changedfields.push('productname');
console.log(changedfields.length);

Javascript numerical indexed array length Javascript数字索引数组长度
So the Javascript numerical indexed array length can be calculated this way:所以Javascript数字索引数组长度可以这样计算:

console.log(array.length);
console.log(changedfields.length); // in your case 

The Javascript associative array length Javascript 关联数组长度
The Javascript associative array (object) length can be calculated following ways: Javascript 关联数组(对象)长度可以通过以下方式计算:
Option 1:选项1:

Object.len = function(obj) {
var objLen = 0;
for (i in obj) {
   obj.hasOwnProperty(i) ? objLen++ : '';
}
return objLen;
};
console.log(Object.len(changedfields));

Option 2:选项 2:

console.log(Object.keys(array).length);
console.log(Object.keys(changedfields).length); // in your case

Note: This has issues with Internet Explorer 8, Opera etc注意:这与 Internet Explorer 8、Opera 等有关

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

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