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

145
Vistas
Add array elements to list

I am trying to append array elements to the list using below code:

function GetList() {
let list = [];
$.ajax({
    url: '../Lookup/GetList',
    type: 'GET',
    cache: true,
    dataType: "json",
    success: function (response) {
        debugger;
        
        list = response;

        //for (var data in response) {
        //    list = { value: data, rest: list };
        //}
        //for (let i = response.length - 1; i >= 0; i--) {
        //    list = { value: array[i], rest: list };
        //}
        //for (let i = response.length - 1; i >= 0; i--) {
        //    list.push(response[i]);
        //}                         
    }        
});
return list;
}

I have attached the screenshot of array data format. I need in the below list format:

var list= [
  {
    "id": "1",
    "LookupMasterName": " Source1",
    "Description": "xxx",       
    "Enabled Flag":"Y"
  },
  {
    "id": "2",
    "LookupMasterName": " Source2",
    "Description": "yyy",        
    "Enabled Flag": "Y"
  }    
];

Above code doesn't work. It returns empty list. Can you help?

enter image description here

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It's returning an empty list because this line return list; is executed before Ajax is completed. A solution to fix it is wrapping your Ajax request into a Promise:

Example:

function GetList() {
  let list = [];
  return new Promise((resolve) => {
    $.ajax({
      url: "../Lookup/GetList",
      type: "GET",
      cache: true,
      dataType: "json",
      success: function (response) {
        list = response;
        resolve(list);
      },
    });
  });
}

Then calling GetList:

GetList().then(list => /* do whatever you need */)
over 4 years ago · Santiago Trujillo Denunciar
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