在 Javascript 中一次迭代两个数组

发布于 2024-09-01 12:44:03 字数 742 浏览 4 评论 0原文

我想同时迭代两个数组,因为数组 A 中任何给定索引 i 的值对应于数组 B 中的值。

我当前正在使用此代码,并获取 当我调用 alert(queryPredicates[i])alert(queryObjects[i]) 时,未定义
当我在调用此函数之前打印出数组时,我知道我的数组已填充。

//queryPredicates[] and queryObjects[] are defined above as global vars - not in a particular function, and I have checked that they contain the correct information.

function getObjectCount(){
    var variables = queryPredicates.length; //the number of variables is found by the length of the arrays - they should both be of the same length
    var queryString="count="+variables;
    for(var i=1; i<=variables;i++){
        alert(queryPredicates[i]);
        alert(queryObjects[i]); 
    }

I want to iterate over two arrays at the same time, as the values for any given index i in array A corresponds to the value in array B.

I am currently using this code, and getting undefined when I call alert(queryPredicates[i]) or alert(queryObjects[i]).
I know my array is populated as I print out the array prior to calling this.

//queryPredicates[] and queryObjects[] are defined above as global vars - not in a particular function, and I have checked that they contain the correct information.

function getObjectCount(){
    var variables = queryPredicates.length; //the number of variables is found by the length of the arrays - they should both be of the same length
    var queryString="count="+variables;
    for(var i=1; i<=variables;i++){
        alert(queryPredicates[i]);
        alert(queryObjects[i]); 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一遍 2024-09-08 12:44:03

任何数组的 length 属性的值都是元素的实际数量(更准确地说,是最大的现有索引加一)。

如果您尝试访问此索引,它将始终为未定义,因为它超出了数组的边界(这种情况发生在循环的最后一次迭代中,因为i<=variables 条件)。

在 JavaScript 中,索引的处理范围是 0length - 1

除此之外,请确保两个数组具有相同数量的元素。

The value of the length property of any array, is the actual number of elements (more exactly, the greatest existing index plus one).

If you try to access this index, it will be always undefined because it is outside of the bounds of the array (this happens in the last iteration of your loop, because the i<=variables condition).

In JavaScript the indexes are handled from 0 to length - 1.

Aside of that make sure that your two arrays have the same number of elements.

梦毁影碎の 2024-09-08 12:44:03

如果 queryPredicates 没有数字索引,例如 0、1、2 等。那么当第一个项目的索引为queryPredicates['some_index'] 不会发出任何警报。

尝试使用 for 循环代替:

stuff['my_index'] = "some_value";

for (var i in stuff)
{
    // will alert "my_index"
    alert(i);

    // will alert "some_value"
    alert(stuff[i]);
}

If queryPredicates does not have numerical indexes, like 0, 1, 2, etc.. then trying to alert the value queryPredicates[0] when the first item has an index of queryPredicates['some_index'] won't alert anything.

Try using a for loop instead:

stuff['my_index'] = "some_value";

for (var i in stuff)
{
    // will alert "my_index"
    alert(i);

    // will alert "some_value"
    alert(stuff[i]);
}
仄言 2024-09-08 12:44:03

JS 中的数组是从零开始的。长度是实际计数。您的循环超出了数组的范围。

Arrays in JS are zero based. Length is the actual count. Your loop is going outside the bounds of the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文