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);
  }
4
  • need little clarification here .. did you set component.set("v.UserIdd" , []); and it made v.userid as null also Commented Nov 13, 2017 at 9:43
  • @ManjotSingh: UserIdd is becoming empty when i am slicing userlis.
    – Salesforce
    Commented Nov 13, 2017 at 9:55
  • why are you using slice. slice is mutable thats might be problem. cannot you just use userlis = [] ? Commented Nov 13, 2017 at 10:04
  • @ManjotSingh: let me check
    – Salesforce
    Commented Nov 13, 2017 at 10:26

3 Answers 3

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .