如何向对象添加数据?

时间:2022-04-05 19:40:49

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,

我有一个API,可以接受单个数据或批量数据,然后相应地发送响应。发送单个数据时,我发送的数据如下,

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,

上面的代码工作得很好,我得到了适当的响应。现在,当我想一次验证多于1条记录时,listData应该是这样的,

{
    "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:

如您所见,listData包含多条记录的详细信息。我无法弄清楚如何将这些多个数据耦合到一个对象并发送到API。仅供参考,contactid,url,companyName和complink的值存储在数组中。例如:

var contactid = [1,2,3,4,5]
var companyName = ["abc","xyz","qwe","asd","zxc"];

1 个解决方案

#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.

如果listData具有正确的格式,只需将其直接附加到您的请求即可。

$.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'
       });
   }

});

});

#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.

如果listData具有正确的格式,只需将其直接附加到您的请求即可。

$.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'
       });
   }

});

});