简体   繁体   English

Javascript / jQuery:嵌套循环,防止覆盖对象属性

[英]Javascript/jQuery: nesting loops, prevent overwriting object property

I have a function which should built an array with keys. 我有一个功能,应该建立一个带有键的数组。 Those keys will have a serie of objects as value. 这些键将具有一系列对象作为值。

I've some loops nested in each other and I'm quite near to the solution, but inside the last loop I'm making a mistake and I can't see the solution. 我彼此之间嵌套了一些循环,并且我已经很接近解决方案了,但是在最后一个循环中,我犯了一个错误,并且看不到解决方案。

The function loopt over an array with Id's. 该函数在带有ID的数组上循环。 Those will be the key value for the output array. 这些将是输出数组的键值。 After that it loops over an array with a lot of objects. 之后,它将遍历具有许多对象的数组。 Those objects are having a property 'category'. 这些对象具有属性“类别”。 Some of them have one, others more. 其中一些有一个,其他一些。 So with a for-loop I loop over all the categories. 因此,对于for循环,我遍历所有类别。

If the categorie is the same as the id then it should push the object to the var objs, which will be added to the right key. 如果类别与id相同,则应将对象推送到var objs,然后将其添加到右键。

This is all working, BUT, I want that the objects are saved with only ONE category. 一切正常,但是,我希望对象仅保存一个类别。 So I declared a new var inside the last loop, put the obj inside there and set the obj.category. 因此,我在最后一个循环内声明了一个新的var,将obj放在其中并设置obj.category。 Unfortunately this is overwriting the 'source', array[x].category. 不幸的是,这覆盖了“源” array [x] .category。 This is not good because this occurs the problem that an object with two categories will only be found once in this function and it had to be found twice so it can be saved twice (once by every represent key value). 这不好,因为会发生这样的问题:在此函数中只有两个类别的对象只能被找到一次,并且必须被找到两次,因此可以被保存两次(每个代表键值一次)。

A lot of explanation for a little piece of code... 一小段代码的大量解释...

$.each(unique_id, function(i, el){
    var objs = [];

    for(var x in array)
    {   
        for(var j=0; j<array[x].category.length; j++)
        {
            if(array[x].category[j] == el)
            {
                var obj = array[x];
                obj.category = el;
                objs.push(obj);
            }
        }
    }
    data[el] = objs;
})

What is happening is : both obj and array[x] are pointing to same object. 正在发生的是:obj和array [x]都指向同一个对象。 they are two references that point to same object .Can you try below: 它们是指向同一对象的两个引用。您可以尝试以下操作:

     $.each(unique_id, function(i, el){
             var objs = [];

            for(var x in array)
            {   
               for(var j=0; j<array[x].category.length; j++)
               {
                   if(array[x].category[j] == el)
                   {
                      var obj = {};
                      $.extend(true,obj,array[x]);
                      obj.category = el;
                      objs.push(obj);
                   }
               }
            }
            data[el] = objs;
     });

Also, in javascript variables are function-scoped so even if you declare them inside the inner loop they are visible throughout the function and not only in the inner loop in which you have defined it. 另外,在javascript中,变量是函数作用域的,因此即使您在内部循环中声明它们,它们在整个函数中也是可见的,不仅在定义它的内部循环中可见。 Of course $extend will copy every property that exists on array[x] and the nested objects and their properties as well. 当然,$ extend将复制存在于array [x]上的每个属性以及嵌套对象及其属性。 if you don't want that. 如果您不想要那样。 just use 只是使用

       var obj = {};
       obj.category = array[x].category;

provided category is also not object. 提供的类别也不是对象。

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

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