简体   繁体   中英

How to pass array value to function javascript

I would like to know how to pass value in array to the ajax function in javascript. For each id, call the function in javascript. How to pass each id in array and call the function

var listid=["fund","trans","ins"]; 

getData(id){
var li = $.ajax({
    url: "/en",
    method: 'get',
    global: false,
    async: false,
    data: {
      idvalue: id
    },
    success: function(value) {
      return value;
    }
  }).responseText;
 console.log(JSON.parse(li));

}

您可以使用:

listid.forEach(function (id) { getData(id) });

You could use the map function for arrays. It is a JavaScript function that takes a callback as a parameter. You can pass any function as a callback and it will call it for every value in that array.

Apply loop to your id array.

var listid=["fund","trans","ins"]; 

for(let i = 0, len = listid.length; i < len; i++) {
    getData(listid[i]);
}

you can use $.each for your array value

 var listid=["fund","trans","ins"]; $.each(listid, function( index, value ) { //console.log( index + ": " + value ); getData(value ); //uncomment this for actual implementation }); function getData(id){ var li = id; /*$.ajax({ url: "/en", method: 'get', global: false, async: false, data: { idvalue: id }, success: function(value) { return value; } }).responseText;*/ //console.log(JSON.parse(li)); console.log(li); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

 var listid=["fund","trans","ins"]; for(var i = 0 ; i < listid.length ; i++){ getData(listid[i]); } function getData(id){ var li = $.ajax({ url: "/en", method: 'get', global: false, async: false, data: { idvalue: id }, success: function(value) { return value; } }).responseText; console.log(JSON.parse(li)); } 

You can iterate over the list using a forEach easily.

var listid=["fund","trans","ins"]; 

listid.forEach(function(id) {
   getData(id);
});

function getData(id) {
   var xhr = $.ajax({
    url: "/en",
    method: 'get',
    global: false,
    async: false,
    data: {
      idvalue: id
    },
    success: function(value) {
      return value;
    }
  }).always(function(data,  textStatus, jqXHR) {
      // deal with the data
  })
}

If you are using newer version of Jquery then The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

function getData(id) {
   var xhr = $.ajax({
    url: "/en",
    method: 'get',
    global: false,
    async: false,
    data: {
      idvalue: id
    },
    success: function(value) {
      return value;
    }
  })
  .done(function(data, textStatus, jqXHR ) {
    // do something with value
  })
  .fail(function(jqXHR, textStatus, errorThrown) {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM