Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda