如何将breadcrumb添加到现有的JSON+LD列表中

How can i add a breadcrumb to an existing JSON+LD list

本文关键字:JSON+LD 列表 breadcrumb 添加      更新时间:2023-09-26

我有一个JSON+LD对象,我需要向列表元素动态添加项。如果我从这个代码开始:

var jsonObj = {
    "@context": "http://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement":
    [
        {
            "@type": "ListItem",
            "position": 1,
            "item":
            {
                "@id": "http://domain.com/",
                "name": "Home"
            }
        },
        {
            "@type": "ListItem",
            "position": 2,
            "item":
            {
                "@id": "http://domain.com/used-equipment/",
                "name": "Used Equipment"
            }
        }
    ]
}

如何使用JavaScript在位置3添加另一个项目?这可能吗?

当然,您添加它就像添加前两个一样:

[
    {
        "@type": "ListItem",
        "position": 1,
        "item":
        {
            "@id": "http://domain.com/",
            "name": "Home"
        }
    },
    {
        "@type": "ListItem",
        "position": 2,
        "item":
        {
            "@id": "http://domain.com/used-equipment/",
            "name": "Used Equipment"
        }
    },
    {
        "@type": "ListItem",
        "position": 3,
        "item":
        {
            "@id": "Insert URL here",
            "name": "Name of the page"
        }
    }
]