The problem:
I have a large string (it's HTML format) and I need to select multiple substrings from it, and then push them into an array of objects. Please note: I cannot change the original string. The example below is a simplified version.
What I need:
An array of objects containing information about "recipes", like so:
const recipes = [
{
title: "This is title number 1",
imagelink: "/exampleimage1.jpg",
recipelink: "www.examplelink1.com"
}, {
title: "This is title number 2",
imagelink: "/exampleimage2.jpg",
recipelink: "www.examplelink2.com"
}]
What the string looks like:
This is a simplified version of the string. The original one contains more characters and more tags like href etc. So I can't only filter on these substrings. As you can see, the title, link and link to the image are present more than once. It therefore doesn't matter which duplicate is pushed into the array, as long as they belong to their 'parent' recipe.
<section class="item recipe " data-title="This is title number 1" data-record-type="recipe">
<figure class="">
<a href="www.examplelink1.com">
<span class="favorites-wrapper">
<span class="favorite shadow " data-is-favorite="false" data-recipe-id="11111" data-recipe-title="This is title number 1">
</span>
</span>
<span class="type">recept</span>
<img src="images/blank-16x11.gif" data-src="/exampleimage1.jpg" class="js-lazyload" data-retina-src="/exampleimage1.jpg" alt="This is title number 1">
</a>
</figure>
<header>
<h4> <a href="www.examplelink1.com"> This is title number 1 </a> </h4>
<div class="rating">
<span><div class="icon icon-star active"></div></span>
<span><div class="icon icon-star active"></div></span>
<span class="count">(<span>84</span>)</span>
</div>
<div class="time">15 min.</div>
</header>
</section>
<section class="item recipe " data-title="This is title number 2" data-record-type="recipe">
<figure class="">
<a href="www.examplelink2.com">
<span class="favorites-wrapper">
<span class="favorite shadow " data-is-favorite="false" data-recipe-id="22222" data-recipe-title="This is title number 2">
</span>
</span>
<span class="type">recept</span>
<img src="images/blank-16x11.gif" data-src="/exampleimage2.jpg" class="js-lazyload" data-retina-src="/exampleimage2.jpg" alt="This is title number 2">
</a>
</figure>
<header>
<h4> <a href="www.examplelink2.com"> This is title number 2 </a> </h4>
<div class="rating">
<span><div class="icon icon-star active"></div></span>
<span><div class="icon icon-star active"></div></span>
<span class="count">(<span>84</span>)</span>
</div>
<div class="time">15 min.</div>
</header>
</section>
I'm not sure if my JavaScript knowledge extends far enough to solve this. Help is much appreciated!