【发布时间】:2021-12-30 05:12:59
【问题描述】:
我有一个数组 [name1,item1,name2,item2,name3...] 需要映射到
{
"@type": "ListItem",
position: index + 1,
name: ,
item: ,
}
我尝试将其拆分为两个单独的数组,例如:
var nameBreadCrumbStructuredData = splitBreadCrumbStructuredData.filter(
function (value, index, Arr) {
return index % 2 == 0;
}
);
var urlBreadCrumbStructuredData = splitBreadCrumbStructuredData.filter(
function (value, index, Arr) {
return index % 2 == 1;
}
);
var faqStructuredDataSplit = nameBreadCrumbStructuredData.map((i) => {
urlBreadCrumbStructuredData.map((j) => ({
"@type": "ListItem",
position: "",
name: i,
item: j,
}));
});
但不幸的是它不起作用。我尝试了一些不同的方法,也尝试了 forEach,但我被卡住了。任何人都可以帮助我如何循环一个数组,但每隔一个或两个数组同时循环一次?感谢您的帮助!
因此,我需要通过映射得到这个:
const breadCrumbStructuredData = {
"@context": "https://schema.org",
"@type": "BreadcrumbList",
itemListElement: [
{
"@type": "ListItem",
position: 1,
name: "nameExample",
item: "https://example.com/",
},
{
"@type": "ListItem",
position: 2,
name: nameExampl1,
item: "https://example1.com/",
},
{
"@type": "ListItem",
position: 3,
name: nameExamp2,
item: "https://example2.com/",
},
{
"@type": "ListItem",
position: 4,
name: nameExamp3,
item: "https://example3.com/",
},
],
};
【问题讨论】:
-
你能指定(或举例)你的输入数组吗?
标签: javascript arrays loops dictionary foreach