Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
How to convert an array into an Object property in jquery/javascript

I have a complex object in a SPA javascript that I need to send to the Api using Ajax. One of the properties is an array and I'm trying to map an array to the id property before sending the data to the Api. Here's the code:

This is the class (the id is a string in the database):

var ContactList = {
     AcntNumber: 0,
     Contacts: [
       {
        id: "",
        name: ""
      }
   ]
}

Then in another file, I'm getting the values from a dropdown and creating an array contacts. I'm having hard time to assign the array to the id of the ContactList.Contacts.

           var users = [];
            var ContactList = Object.assign({}, ContactList);

            $('select#users').each(function () {
                Array.prototype.push.apply(users, $(this).val());
            });
                                                                      
            ContactList.Contacts = Object.assign({}, users);

            ContactList.AcntNumber = $("#accNum").val();

            $.ajax({
                type: "POST",
                headers: { "Authorization": `Bearer ${token}` },
                async: false,
                url: URL,
                data: JSON.stringify(ContactList),
                contentType: "application/json",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                }
            });

I keep getting the following error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable.

Here's my Classes in the Api:

public class ContactList
{        
    public IEnumerable<Contact> Contacts { get; set; }
    public Accounts Account { get; set; }
}  

public class Contact
{        
    public string ID { get; set; }
    
    public string Name { get; set; }
}

Any help would be greatly appreciated, thanks!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!