jQuery每个数组问题

发布于 2024-12-05 04:59:27 字数 824 浏览 0 评论 0原文

我正在使用 jquery every 函数循环遍历数组。当我使用拼接修改原始数组时,我为其分配一个临时变量而不是实际的数组本身来循环。然而,即使当我拼接 array 时,temp 似乎也会被修改。

function example (Data, index, array) {
            var temp = array;
            $.each(temp, function(i, v) {    
                if(Data["b"+v].length > index) {
                    //do stuff
                } else {
                    array.splice(i,1);
                }
            });
            if(array.length > 0) {
                example(Data, index+1, array);
            }
}
array = [1,2,3,4]
Data = {"b1":[a,b,c,d],"b2":[e,f,g,h], "b3":[i,j], "b4":[k,l,m,n]};
example(Data, 0, array);

在示例的第三次调用中,在 temp 的第四次迭代中,v 变为未定义,因此下一行会输出“无法读取未定义的长度”的错误。这种情况发生在 array.splice(3,1) 被调用之后,看起来 temp 指向与 array 相同的位置,而不是它的副本。
有人可以帮忙吗?

I'm looping through an array using the jquery each function. I assign a temp variable for it to loop through instead of the actual array itself as I am modifying the original array using splice. However, it looks like temp is getting modified even when I splice array.

function example (Data, index, array) {
            var temp = array;
            $.each(temp, function(i, v) {    
                if(Data["b"+v].length > index) {
                    //do stuff
                } else {
                    array.splice(i,1);
                }
            });
            if(array.length > 0) {
                example(Data, index+1, array);
            }
}
array = [1,2,3,4]
Data = {"b1":[a,b,c,d],"b2":[e,f,g,h], "b3":[i,j], "b4":[k,l,m,n]};
example(Data, 0, array);

On the third call of example, on the 4th iteration of temp, v becomes undefined and therefore the next line pumps out an error of "cannot read length of undefined". This happens right after array.splice(3,1) is called which seems like temp is pointing to the same place as array instead of being a copy of it.

Can anyone help?

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

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

发布评论

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

评论(3

顾铮苏瑾 2024-12-12 04:59:27

数组和对象是通过引用分配的。 temparray 引用同一个数组。您可以使用 .slice() [MDN]

var temp = array.slice();

您可以按相反的顺序迭代数组,而不是创建副本:

for(var i = array.length; i--; ) {
    if(Data["b"+array[i]].length > index) {
        //do stuff
    } else {
        array.splice(i,1);
    }
}

Arrays and objects are assigned by reference. temp and array reference the same array. You can create a shallow copy using .slice() [MDN]:

var temp = array.slice();

Instead of creating a copy, you could iterate over the array in reverse order:

for(var i = array.length; i--; ) {
    if(Data["b"+array[i]].length > index) {
        //do stuff
    } else {
        array.splice(i,1);
    }
}
冧九 2024-12-12 04:59:27

temp 只是对同一个数组的引用,因此 temp 和 array 是同一件事。您想要复制一份,如下所示:

temp = array.slice(0);

temp is just a reference to the same array, so temp and array are the same thing. You want to make a copy, like so:

temp = array.slice(0);
孤独难免 2024-12-12 04:59:27

JavaScript 中的赋值是通过引用进行的,它不会复制对象。例如...

var obj1 = {};
var obj2 = obj1;
obj2.hello = "world";
console.log( obj1.hello ); // logs "world"

这是因为 obj1 和 obj2 指向内存中的同一个对象。

如果你想复制一个数组,slice< /code>方法可以使用...

var arrayCopy = myArray.slice(0)

现在 arrayCopy & myArray 可以独立编辑。但是,请注意,虽然数组本身是独立的,但它们指向相同的对象......

arrayCopy[0] === myArray[0]; // true
arrayCopy[0] = {my: "new object"};
arrayCopy[0] === myArray[0]; // now false

Assignments in JavaScript are by reference, it doesn't copy the object. Eg...

var obj1 = {};
var obj2 = obj1;
obj2.hello = "world";
console.log( obj1.hello ); // logs "world"

This is because obj1 and obj2 are pointing to the same object in memory.

If you want to make a copy of an array, the slice method can be used...

var arrayCopy = myArray.slice(0)

Now arrayCopy & myArray can be edited independently. However, be aware that although the arrays themselves are independent, they point to the same objects...

arrayCopy[0] === myArray[0]; // true
arrayCopy[0] = {my: "new object"};
arrayCopy[0] === myArray[0]; // now false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文