0

I have been trying to build a generic method which can dynamically set attributes via component.set() for example something like:

for(var i=0;i<myObjectArray;++){
    component.set(myObjectArray[i].attName,myObjectArray[i].attValue);
}

But I get undefined errors. I've tried using "v.att" and just v.att but neither work.

Is this possible?

Edit

Here's the combinations I have tried.

Single quotes:

var myObjArray = [];
myObjArray.push({attName:'v.att1', attValue:object.value1});
myObjArray.push({attName:'v.att2', attValue:object.value2});
for(var  i = 0; i < myObjArray.length; i++){
    component.set(myObjArray[i].attName,response);
}

Single in double:

var myObjArray = [];
myObjArray.push({attName:"'v.att1'", attValue:object.value1});
myObjArray.push({attName:"'v.att2'", attValue:object.value2});

Double in single:

var myObjArray = [];
myObjArray.push({attName:'"v.att1"', attValue:object.value1});
myObjArray.push({attName:'"v.att2"', attValue:object.value2});

All end up with the error:

Uncaught Error in $A.getCallback() [Cannot read property 'varName' of undefined]

2
  • I think that should work as long as myObjectArray[i].attName contains the value provider: "v.myAttributeName". Can you provide some sample data within myObjectArray? And of course, that said attribute must exist on the page. If you want to ignore the exceptions, why not consider putting in a try-catch block? Aug 2, 2018 at 17:33
  • component.set expects the attribute name enclosed in quotes. So that's where you may like to have the myObjectArray[i].attName rendered enclosed accordingly.
    – Jayant Das
    Aug 2, 2018 at 17:45

1 Answer 1

0

Some stupidity on my side here. The dynamic set fails in a callback...so I was trying to refer to array which was undefined in the context of the callback.

You must log in to answer this question.

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