I am currently in the process of creating a new list item in a SharePoint list using the REST API. To make the process more flexible, I am storing the field names in an array that will be dynamically updated by another function. However, I have encountered a roadblock while trying to reuse my existing code. The code is failing at the metadata section where I attempt to iterate through the array and add each element as a field name. Each element in the array corresponds to a field name.
var favorite=[];
//favorite is an array of field titles dynamically updated with another function
//I aim to dynamically populate this array while creating a new list item
var item = {"__metadata": {"type": "SP.Data.TestCatalogListItem"},for(int i=0; i<favourite.length;i++){favorite[i]:cells[i]}};
_createListItem(item);
function _createListItem( listItems,listname, success, failure) {
$.ajax({
url: "https://site/_api/web/Lists/getbytitle('MYLIST')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(listItems),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
//success(data);
},
error: function (data) {
//failure(data);
}
});
}