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

129
Views
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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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 */)
about 4 years ago · Juan Pablo Isaza Report
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!