Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda