Ниже я упомянул динамическую форму объекта js, которую я пытаюсь преобразовать в данные схемы и добавить в заголовок веб-страницы для целей SEO. Не уверен, как это возможно, так как я новичок в этом.
Моя функция JS:
function populateJsonLDScript(data) {
if (typeof (data) != "undefined" && data != null) {
var finalSchemaObj = {};
var tempSchemaItems = [];
for (var i = 0; i < data.length; i++) {
var tempSchemaData = {};
tempSchemaData["@type"] = "ListItem";
tempSchemaData["position"] = data[i].DisplayOrder;
tempSchemaData["item"] = {
"@id": data[i].CanonicalURL,
"name": data[i].Name
};
tempSchemaItems.push(tempSchemaData);
}
for (var i = 0; i < tempSchemaItems.length; ++i) {
finalSchemaObj[i] = tempSchemaItems[i];
}
var scriptSchema = document.createElement('script');
scriptSchema.type = 'application/ld+json';
scriptSchema.text = JSON.stringify({
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [finalSchemaObj]
});
if ($('head script[type = "application/ld+json"]').length > 0) {
$('head script[type = "application/ld+json"]').remove();
$("head").append(scriptSchema);
} else {
$("head").append(scriptSchema);
}
}
}
ВЫХОД
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [{
"0": {
"@type": "ListItem",
"position": 0,
"item": {
"@id": "http://example.com",
"name": "Home"
}
},
"1": {
"@type": "ListItem",
"position": 1,
"item": {
"@id": "http://example.com/jewelry",
"name": "Jewelry"
}
},
"2": {
"@type": "ListItem",
"position": 2,
"item": {
"@id": "http://example.com/jewelry/necklaces-pendants",
"name": "Necklaces & Pendants"
}
}
}]
}
Но все же Google заявил о недопустимом формате. Итак, я хочу отформатировать приведенное выше, чтобы -
{"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":[
{
"@type":"ListItem",
"position":0,
"item": {
"@id":"http://example.com",
"name":"Home"
}
},
{
"@type":"ListItem",
"position":1,
"item":{
"@id":"http://example.com/jewelry",
"name":"Jewelry"
}
},
{
"@type":"ListItem",
"position":2,
"item":{
"@id":"http://example.com/jewelry/necklaces-pendants",
"name":"Necklaces & Pendants"
}
}
]
}
Кто-нибудь, пожалуйста, помогите, как это сделать?



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Не используйте объект, используйте массив и вставьте в него. Что-то вроде этого:
function populateJsonLDScript(data) {
if (typeof(data) != "undefined" && data != null) {
var finalSchemaObj = []; // <----- use array here
var tempSchemaItems = [];
for (var i = 0; i < data.length; i++) {
var tempSchemaData = {};
tempSchemaData["@type"] = "ListItem";
tempSchemaData["position"] = data[i].DisplayOrder;
tempSchemaData["item"] = {
"@id": data[i].CanonicalURL,
"name": data[i].Name
};
tempSchemaItems.push(tempSchemaData);
}
for (var i = 0; i < tempSchemaItems.length; ++i) {
finalSchemaObj.push(tempSchemaItems[i]); // <--- push here
}
var scriptSchema = document.createElement('script');
scriptSchema.type = 'application/ld+json';
scriptSchema.text = JSON.stringify({
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": finalSchemaObj // <----- and use finalSchemaObj here
});
if ($('head script[type = "application/ld+json"]').length > 0) {
$('head script[type = "application/ld+json"]').remove();
$("head").append(scriptSchema);
} else {
$("head").append(scriptSchema);
}
}
}