0

I have an API which can accept single data or bulk data and then send the response accordingly. While sending a single data I'm sending the data as below,

var contactId = 123;
var url = "www.abc.com";
var companyName = "ABC";
var compLink = "/abc/123/link";
var randomKey = generateRandomKey();

   $.ajax({
       url:liurl,
       type:"POST",
       data:JSON.stringify({
           "verification_key":randomKey,
           "listData" :
               [{
               "contactid":contactId,
               "url": url,
               "li_company":companyName,
               "complink":compLink
           }]
       }),
       contentType:"application/json",
       dataType:"json",
       success: function(result){
           $.each(result, function(index, element) {
               //perform operation on 'result'
           });
       }
   });

The above code works perfectly and I get the appropriate response. Now when I want to verify more than 1 record at a time the listData should be like this,

{
    "verification_key":"d0vvNl04dk3y",
    "listData" :
        [{
        "contactid":"241",
        "url":"http://www.name.com/in/daveandrews",
        "companyName":"Devious Media",
        "complink":"/company/devious-media"
    },
    {
        "contactid":"242",
        "url":"http://www.name.com/in/something",
        "companyName":"Sol Media",
        "complink":"/company/somemedia"
    },{
        "contactid":"243",
        "url":"http://www.name.com/in/daveandrews",
        "companyName":"Mega Media",
        "complink":"/company/xyzmedia"
    }]
}

As you can see the listData contains details for more than 1 record. and I can't figure out how to couple these multiple data into a single object and send to the API. FYI, the values for contactid, url, companyName and complink is stored in an array. Eg:

var contactid = [1,2,3,4,5]
var companyName = ["abc","xyz","qwe","asd","zxc"];
3
  • You need to iterate through the first array and use the index of the loop to pull in the correct value from the second (or more) arrays - this is assuming they will always be in perfect order - have a look at this for starters developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Nov 18, 2016 at 17:21
  • What do you mean by "couple these multiple data into a single object"? Because the important thing is, what does your API expect? I would think that it expects exactly what you're showing in your second code block, but of course, I can't know that. Commented Nov 18, 2016 at 17:25
  • Yes, the API accepts the data as shown in the second code block Commented Nov 18, 2016 at 17:29

1 Answer 1

2

Are you looking for something clever, or is it this simple? I don't think there's a magic functional approach to this, but maybe others are more clever than I am.

Assuming your arrays are the same length, something like this:

let listData = [];

for (let i=0; i < contactid.length; i++) {
    listData.push({
        contactid: contactid[i],
        url: "http://www.name.com/in/something",
        li_companyName: companyName[i],
        complink: complink[i]
    });
}

(update with how to assemble the new request)

with listData having the correct format, just attach it to your request directly.

$.ajax({
   url:liurl,
   type:"POST",
   data:JSON.stringify({
       "verification_key":randomKey,
       "listData" : listData
   }),
   contentType:"application/json",
   dataType:"json",
   success: function(result){
       $.each(result, function(index, element) {
           //perform operation on 'result'
       });
   }

});

Sign up to request clarification or add additional context in comments.

1 Comment

This is very similar to what I'm looking for, can you tell me how to add 'verification_key' along with this as shown in my second code block. Keeping in mind that the verification_key will be added only once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.