Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mapping through array with jsx syntax

I am having trouble with mapping through an array so i can add the reviews to the review structured data, the JSX is not valid. i wonder if it’s the perfect way or i need to do this in a function before calling it!

<MetaTags>
    { this.state.name ? (
        <script type="application/ld+json">
        {`
            {
                "@context": "https://schema.org/",
                "@type": "LocalBusiness",
                "aggregateRating": {
                "@type": "AggregateRating",
                    "ratingValue": "${this.state.rating}",
                    "ratingCount": "${this.state.voters}"
                },
                "review": [
                    {
                        "@type": "Review",
                        "author": "Ellie",
                        "datePublished": "2011-04-01",
                        "reviewBody": "The lamp burned out and now I have to replace it.",
                        "name": "Not a happy camper",
                        "reviewRating": {
                          "@type": "Rating",
                          "bestRating": "5",
                          "ratingValue": "1",
                          "worstRating": "1"
                        }
                    },
                    {
                        "@type": "Review",
                        "author": "Ellie",
                        "datePublished": "2011-04-01",
                        "reviewBody": "The lamp burned out and now I have to replace it.",
                        "name": "Not a happy camper",
                        "reviewRating": {
                          "@type": "Rating",
                          "bestRating": "5",
                          "ratingValue": "1",
                          "worstRating": "1"
                        }
                    }
                ]
            }
        `}
        </script>
    ) : ''}
</MetaTags>

i have tried to wrapp the map function with {“}

"review": [
    {` 
        this.state.posts ? this.state.posts.slice(0, 10).map(data => (
            {
                "@type": "Review",
                "author": "Ellie",
                "datePublished": "2011-04-01",
                "reviewBody": "The lamp burned out and now I have to replace it.",
                "name": "Not a happy camper",
                "reviewRating": {
                    "@type": "Rating",
                    "bestRating": "5",
                    "ratingValue": "1",
                    "worstRating": "1"
                }
            },
        )) : '';
    `}
]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

"review": this.state.posts ? this.state.posts.slice(0, 10).map(data => ({
  "@type": "Review",
  "author": "Ellie",
  "datePublished": "2011-04-01",
  "reviewBody": "The lamp burned out and now I have to replace it.",
  "name": "Not a happy camper",
  "reviewRating": {
    "@type": "Rating",
    "bestRating": "5",
    "ratingValue": "1",
    "worstRating": "1"
  }
})) : []
<MetaTags>
  {this.state.name ? (
    <script type="application/ld+json">
      {`
            {
                "@context": "https://schema.org/",
                "@type": "LocalBusiness",
                "aggregateRating": {
                "@type": "AggregateRating",
                    "ratingValue": "${this.state.rating}",
                    "ratingCount": "${this.state.voters}"
                },
                "review": ${
                  this.state.posts
                    ? this.state.posts.slice(0, 10).map((data) => ({
                        "@type": "Review",
                        author: "Ellie",
                        datePublished: "2011-04-01",
                        reviewBody:
                          "The lamp burned out and now I have to replace it.",
                        name: "Not a happy camper",
                        reviewRating: {
                          "@type": "Rating",
                          bestRating: "5",
                          ratingValue: "1",
                          worstRating: "1",
                        },
                      }))
                    : []
                }
            }
        `}
    </script>
  ) : (
    ""
  )}
</MetaTags>;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading