Currently in the process of scraping data from various websites and looping through it. Specifically focusing on scraping the head section.
This is an example illustrating the structure of the data:
const head = {
rel_data: [
{
rel: "rel",
items: [
{
type: "type",
sizes: "sizes",
href: "href"
}
]
}
]
};
The goal is to insert data into items
whenever a match is found for the rel
.
$('head link').each(function(index) {
if(head?.rel_data[index]?.rel == rel) {
head?.rel_data[index]?.items.push({
type: (type !== undefined) ? type : null,
sizes: (sizes !== undefined) ? sizes : null,
href: (href !== undefined) ? href : null
});
} else {
head.rel_data.push({
rel: (rel !== undefined) ? rel : null,
items: [
{
type: (type !== undefined) ? type : null,
sizes: (sizes !== undefined) ? sizes : null,
href: (href !== undefined) ? href : null
}
]
});
}
})
Here is what is expected:
rel_data: [
{
rel: "icon",
items: [
{
type: "type",
sizes: "sizes",
href: "href"
},
{
type: "type",
sizes: "sizes",
href: "href"
}
]
},
{
rel: "other-rel-type",
items: [...]
}
]
However, the result obtained is not as expected:
rel_data: [
{
rel: "icon",
items: [
{
type: "type",
sizes: "sizes",
href: "href"
}
]
},
{
rel: "icon",
items: [
{
type: "type",
sizes: "sizes",
href: "href"
}
]
}
]
When replacing index
with 0
, it functions correctly for the initial type of rel
(such as icon) but not for others?