如何动态添加数组,如下面的代码所示。我在JavaScript中创建了一个多维数组但是如何推送src
"/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg"
和title
"MyTitle"
。我有多个值,我正在迭代它们?
carousel_images_info = [
{ src: "/_layouts/15/My.Portal/Images/my1.jpg", title: "ssnit01" },
{ src: "/_layouts/15/my3.Portal/Images/my2.jpg", title: "ssnit02" },
{ src: "/_layouts/15/my4.Portal/Images/my3.jpg", title: "ssnit03" },
{ src: "/_layouts/15/my5.Portal/Images/my4.jpg", title: "ssnit04" },
{ src: "/_layouts/15/my5.Portal/Images/ssnit05.jpg", title: "ssnit05" }
];
答案 0 :(得分:1)
推送:)
carousel_images_info.push({
title: "MyTitle",
src: "/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg"
});
但是,如果要避免重复,则必须在添加新行之前遍历整个列表以执行比较。另一个解决方案可能是推送所有内容,然后仅在需要时删除重复项。
答案 1 :(得分:1)
这是包含对象的数组,你需要将对象推送到它,如:
var new_obj = {
src: "/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg",
title: "My Title"
};
carousel_images_info.push(new_obj);
答案 2 :(得分:0)
src和title的值是动态的,我可以这样做吗?
var item_enumerator = this.items.getEnumerator();
while (item_enumerator.moveNext()) {
var item = item_enumerator.get_current();
var image_url = location.protocol + "//" + location.host + item.get_item('FileRef');
var image_title = item.get_item('FileLeafRef');
var set = {
src: image_url,
title : image_title
};
this.carousel_images_info.push(set);
}
答案 3 :(得分:0)
试试这个,
var new_obj = {};
new_obj.src = "/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg";
new_obj.title= "MyTitle";
carousel_images_info[carousel_images_info.length] = new_obj
如果你动态地做这个,那就做一个函数,
carousel_images_info=[];
function Insert(xSrc,xTitle)
{
var new_obj = {};
new_obj.src = xSrc;
new_obj.title= xTitle;
carousel_images_info[carousel_images_info.length] = new_obj;
}
Insert("/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg","MyTitle");