0

Can someone please point out what is wrong. I have some code that produces a simple object and adds it to an array, but the problem is that the last value overwrites all other value

      let search = []
      let XMLSearch = { }
      XMLSearch.First =  'Bob'  
      XMLSearch.Last =  'Smith'                 
      search.push(XMLSearch)
      XMLSearch.First =  'Mary'  
      XMLSearch.Last =  'Smith'                   
      search.push(XMLSearch)
      XMLSearch.First =  'Joe'  
      XMLSearch.Last =  'Jones'                   
      search.push(XMLSearch)

I'm sending the search as the payload in an AXIOS post to a Web 2.0 API as a

     <Route("api/pmid_Impact/PostValue")> Public Function PostValue(<FromBody()> ByVal data As List(Of XMLSearch)) As HttpResponseMessage

and it comes in as a List of

       Public Class XMLSearch
        Public Property First As String
        Public Property Last  As String  
       End Class

But everyone is Joe Jones. So I am assuming that I am overwriting my object before it leave my JavaScript with the last value put in. How can I fix this? Thanks I'm showing three names as a sample but I will really need to loop over hundreds to make the search payload.

Bill
  • 1,423
  • 2
  • 27
  • 51

1 Answers1

1

Try this

 let search = []
 let XMLSearch = { }
 XMLSearch.First =  'Bob'  
 XMLSearch.Last =  'Smith'                 
 search.push({...XMLSearch})
 XMLSearch.First =  'Mary'  
 XMLSearch.Last =  'Smith'                   
 search.push({...XMLSearch})
 XMLSearch.First =  'Joe'  
 XMLSearch.Last =  'Jones'                   
 search.push({...XMLSearch})
  • This is the perfect answer that I was looking for. Simply three little dots. Thanks. Glad to have you around – Bill Aug 02 '22 at 14:34