简体   繁体   English

无法返回在JavaScript中创建的数组的长度

[英]Unable to return length of array created in javascript

I created an array which contains query string values from url 我创建了一个数组,其中包含来自url的查询字符串值

var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

for (var i = 0; i < hashes.length; i++) {
     hash = hashes[i].split('=');
     vars[hash[0]] = hash[1];
}

If I want to iterate it, won't happens anything: 如果我想对其进行迭代,将不会发生任何事情:

$.each(query_array, function(k, v) {
   console.log(v);
}); 

Then I wrote 然后我写

console.log(query_array.length);

But my array looks like in Chrome console: 但是我的数组看起来像在Chrome控制台中:

[keyword: "mercedes", redirectTo: "test.aspx", remove: function]

The length returns 0. Why ? 长度返回0。为什么?

How could I iterate through this kind of array ? 我如何遍历这种数组?

If you make vars an object , not an array it will work fine for you: 如果您将vars object而不是array那么它将对您很好:

var vars = {}, hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

for (var i = 0; i < hashes.length; i++) {
     hash = hashes[i].split('=');
     vars[hash[0]] = hash[1];
}

console.log(vars) //Object {keyword: "mercedes", redirectTo: "test.aspx", remove: function}

You cannot access an array's element by an index, that is not a number. 您不能通过索引(不是数字)来访问数组的元素。 In your case - the index is not a number. 就您而言-索引不是数字。

var vars = [] should be changed to var vars = {} as your creating Object. var vars = []应该更改为var vars = {}作为创建对象。 [] represents Array. []代表Array。

Then, $.each will work. 然后, $.each将起作用。

Also you can get query string using location.search instead of location.href. 您也可以使用location.search而不是location.href获取查询字符串。 Please check the updated code below 请检查下面的更新代码

var vars = {}, hash;
var hashes = window.location.search.substring(1).split('&');

for (var i = 0; i < hashes.length; i++) {
     hash = hashes[i].split('=');
     vars[hash[0]] = hash[1];
}

vars is not an array. vars不是数组。 In javascript arrays must always have integer indexes. 在javascript中,数组必须始终具有整数索引。 In your case you using strings as indexes: 在您的情况下,您使用字符串作为索引:

vars[hash[0]] = hash[1];

So don't create an array, just create an object: 因此,不要创建数组,而只需创建一个对象:

var vars = {}, hash;

and you will be able to iterate over the properties of this object: 并且您将能够遍历此对象的属性:

$.each(vars, function(k, v) {
    console.log(k + ': ' + v);
});

Ok, I'll give a detailed explanation for this. 好的,我将对此进行详细说明。

Every object in JavaScript can act like a Map(Dictionary). JavaScript中的每个对象都可以充当Map(Dictionary)。 Something like this. 这样的事情。

var a = new Object();
a["foo"] = 10;
a["bar"] = 20;

... ...

Even an Array ( [] ) is an object. 甚至Array[] )都是对象。 So it will respect the same behavior. 因此,它将尊重相同的行为。 Like this... 像这样...

var arr = [];
arr["foo"] = 10;
arr["bar"] = 20;

But this does not mean you are using the Array as it is supposed to be used. 但这并不意味着您正在使用应该使用的数组。 Even though you have added properties to your Array, you have NOT added any element to the array and because of that the length count is 0. So maybe you should keep the count as a separate key/value pair. 即使您已经向数组添加了属性,也没有向数组添加任何元素,因此长度计数为0。因此,也许应该将计数保留为单独的键/值对。

Hope you understood. 希望你能理解。

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

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