When working on a project, it's very common to send complex data types from Lightning Web Components to Apex methods.
When it comes to sending primitive data types from LWC to Apex it's pretty much straight forward.
Assuming the apex method receives a param with the name searchText this is how the LWC code is going to look like.
In LWC we are sending data as an Object {searchText: 'United'} and It's a mandate that the key in this object is matching the Apex method param name.
Here is the Apex class!
Now the catch is what if we are interested in sending complex data types from LWC to Apex methods.
If we send an Array in LWC then it will be received as a List in Apex method. Likewise, if we send an object in LWC then it will be received as a Map in the Apex method.
Here is some sample code!
Some changes have to be made to Apex class and the refactored Apex class is going to look like this.
Focus on the Array and the Object that we are sending in the LWC. In Apex class they are being received as a List and Map.
The key in an Object doesnโt have quotes and hence in Apex method the Map has this signature <Object, String> (observe how I have used Object for the data type of keys)