0

Consider i have an track array contains object with initial data in Onchange event i'm trying to update the track value but the value getting added everytime in the onchange event.please help me to solvw the issue

@track myList = [{name:'Mylabel1s' , website:'2google.com' ,phone :'74xxx78963',amount : 45 },{name:'Mylabel2s' , website:'2google.com' ,phone :'7xxxx78963',amount : 48 }]


 handleChange(event)
{
     this.myList =[...this.myList ,..event.detail];

}

Its keep adding everytime when handlechange event is fired i need both value mylist intial data with current event value how to achieve this in LWC ?

1 Answer 1

2

The syntax you're currently using is used to concatenate a value to an array. You need to reference the original Array and update the appropriate index:

this.myList[event.target.dataset.key][event.target.dataset.field] = event.target.value;

Here, the target is defined as something like:

<lightning-input label="Field Label" data-key={myListItem.key} data-field="website">
</lightning-input>

I've previously written an example that you can use as a starting point accessing values dynamically. It's not quite the same as what you're doing, but it should be sufficient to get you going.

4
  • my event.detail will contains [{name:'MyChange' ,id:'001',website:'google.com' ,phone :'74xxx78963',amount : 40 }] and my Onchange is dispatch event method. Commented Nov 28, 2021 at 16:21
  • please guide me how to use it correctly Commented Nov 28, 2021 at 16:21
  • @NewLearner If that's the data you have, then you could: const index = this.myList.findIndex(item => item.id === event.detail.id); this.myList[index] = event.detail Commented Nov 28, 2021 at 16:33
  • when i tried above -1: Proxy {0: {…}} i recieved something like this Commented Nov 28, 2021 at 16:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.