我正在尝试为Sharepoint列表中的元数据构造字典,添加一个新项目。我试图动态地做到这一点,而不是对列的名称进行硬编码。
这里是应构造的原始项目属性元数据。
itemProperties={"__metadata":{"type":"SP.Data.TestingListItem"},"fldname1":"Value1","fldname1":"Value2","fldname3":"value3"};
这是我到目前为止所拥有的
var itemProperties={"__metadata":{"type":"SP.Data.TestingListItem"}};
我在这里运行一个循环,直到j等于5;
itemProperties=itemProperties+'"'+favorite[j]+'"'+':'+'"'+cells[j]+'"'+','
//favorite has the column name and cells has the values for those columns
我尝试构建一个字符串并添加多个值,但是它没有按预期的方式工作。
itemProperties.push("Title":"AMA-424"); or
itemProperties.append("Title":"AMA-424");**
我都尝试过,但是它们无效。我的想法不多了。我尝试将整个内容构建为字符串,但create list item函数不会采用字符串。
非常感谢任何帮助或指导。
如果有一个对象,则可以通过添加更多的键/值对来添加。这样做是这样的:
var obj = {};
obj['myKey'] = "myValue";
请考虑以下示例。
var itemProperties = {
"__metadata": {
"type": "SP.Data.TestingListItem"
},
"fldname1": "Value1",
"fldname1": "Value2",
"fldname3": "value3"
};
var favs = [
"Title",
"Feature",
"Data"
];
var cells = [
"AMA-242",
"One",
"abc"
];
for (var i = 0; i < favs.length; i++) {
itemProperties[favs[i]] = cells[i];
}
console.log(itemProperties);
这是Vanilla JavaScript示例。现在,如果您想要一个jQuery示例,我建议您使用$.extend()
。
描述:将两个或更多对象的内容合并到第一个对象中。
查看更多:https://api.jquery.com/jquery.extend/
这有点像.push()
,但对于对象而言。考虑以下内容。
$(function() {
var itemProperties = {
"__metadata": {
"type": "SP.Data.TestingListItem"
},
"fldname1": "Value1",
"fldname1": "Value2",
"fldname3": "value3"
};
var favs = {
title: "AMA-242",
feature: "One",
data: 123
};
itemProperties = $.extend(itemProperties, favs);
console.log(itemProperties);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>