I have an attibute <aura:attribute name="projectResources" type="Contact[]" /> and iterate it by <aura:iteration>. When user clicks a button I want to add a new element to the array. When I use projectResources.push(selectedResource); it adds the element as a Proxy and do not apply the element inside of <aura:iteration>. To workaround I use projectResources = JSON.parse(JSON.stringify(projectResources));. Example:

var projectResources = component.get("v.projectResources");

// Workaround to extract array from Proxy.
projectResources = JSON.parse(JSON.stringify(projectResources));        

component.set("v.projectResources", projectResources);

I don't like this JSON.parse(JSON.stringify()) approach. Is there any better way?

  • Hi Mikhail, have you already done the trailhead module that covers this topic? trailhead.salesforce.com/modules/lex_dev_lc_basics
    – glls
    Jul 16, 2017 at 14:24
  • 1
    I've directly used get, push, set to add new entries to an <aura:iteration> with the HTML updating as expected. Not sure what you mean by "proxy" here. If you are starting from no items worth adding if (!projectResources) projectResources = []; to start with an empty array not null or undefined.
    – Keith C
    Jul 16, 2017 at 18:14
  • @glls, yes, I passed the trailhead module. May be I missed something there... Can you point me to a place where it explains how to add a new item to an existing <aura:attribute type="anySObjectArray"> to show it inside of <aura:iteration>? Aug 5, 2017 at 9:11
  • Make sure your aura:iteration items attribute binds projectResources ala {!v.projectResources}
    – user54038
    Feb 21, 2018 at 5:30

1 Answer 1

loop : function(component, event, helper) {
 var spaceList = component.get("v.spaceTypeList");
var detail = component.get("v.detailRecord");
var newlst =[];
 for(i in spaceList){
    var space = spaceList[i];
    var detailtemp = {};
    detailtemp = detail;
    detailtemp.Name = space;
 console.log("detail space subset "+detailtemp.Name);
    console.log("value after iteration"+i+JSON.stringify(newlst));

You must log in to answer this question.

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