0

Hi i have two attributes in lightning component which is of type ID[]. In one attribute i am setting id's and in another attribute i am pushing these values, which i want to use later.

In a method i am making empty my first attribute, due to which my second attribute is also getting empty which shouldn't be ideally.

<aura:attribute name="UserId" type="Id[]"/>
<aura:attribute name="UserIdd" type="Id[]"/>

 //pushing the values
  component.set("v.UserIdd" , []);
  var Users = [];
  var us = component.get("v.UserId");
  for(var i=0; i< us.length; i++)
   {
    Users.push(us[i]); 
   }
  component.set("v.UserIdd", Users);

 //making empty
  var userlis = component.get("v.UserId");
  if(userlis != null)
  { 
   userlis.splice(0,userlis.length);
  }
  • need little clarification here .. did you set component.set("v.UserIdd" , []); and it made v.userid as null also – Manjot Singh Nov 13 '17 at 9:43
  • @ManjotSingh: UserIdd is becoming empty when i am slicing userlis. – Effat Naaz Nov 13 '17 at 9:55
  • why are you using slice. slice is mutable thats might be problem. cannot you just use userlis = [] ? – Manjot Singh Nov 13 '17 at 10:04
  • @ManjotSingh: let me check – Effat Naaz Nov 13 '17 at 10:26
0

There is no need of this much code to attain this functionality. You can simply set the value from one attribute to another and clear the first attribute easily. This can be acheived by the following code also.

component.set("v.UserIdd" , component.get("v.UserId"));
component.set("v.UserId", []);

Hope this helps.

0

Hi Effat problem is with mutability in js. Both userId and Useridd attributes are referencing to same reference so thats why when you change one another is also getting changed.

So a quick workaround is deepCloning the attribute before setting it in another attribute.

var us = component.get("v.UserId");
   var deepClonedUs = JSON.parse(JSON.stringify(us));
  for(var i=0; i< deepClonedUs.length; i++)
   {
    Users.push(deepClonedUs[i]); 
   }
  component.set("v.UserIdd", Users);
-1

you first need to add the values in user array, while looking on user showing empty records.

var us = component.get("v.UserId"); 
// us is empty here so no loop run.

  for(var i=0; i< us.length; i++)
   {
    Users.push(us[i]); 
   }
  component.set("v.UserIdd", Users);

hope this helps you.

  • component.get("v.UserId") i have the values, it is not empty initially. – Effat Naaz Nov 13 '17 at 9:56
  • <aura:attribute name="UserId" type="Id[]"/> this is empty as per code – Nagendra Swami Nov 13 '17 at 10:12
  • i am pushing the values afterwards, later i am making it null. – Effat Naaz Nov 13 '17 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.