0

products.json: {"model": "PLM-7-P-21","category":["classic","new"],"fabric":["white","beech","glass"], "shine": "false","size": "45x25","bulb": 2, "id": "5cfa55c5"},`

and then I want to use category and fabric as data-category:const layout = require('../layout');module.exports = ({ products }) => {const renderedProducts = products.map(product => {return <div class="image shopProduct" data-category=${product.category}><img src="data:image/png;base64, ${product.image}"/><h3 class="subtitle">${product.title} ${product.model}</h3><h5>${product.price} zł</h5><form action="/cart/products" method="POST"><input hidden value="${product.id}" name="productId" /><button class="button has-icon is-inverted">Dodaj do<i class="fa fa-shopping-cart"></i></button></form></div>;}).join('\n');` but then I have got a string data-category="classic, new" but I want to get data-category="classic new" without comma... I would be grateful for your help.

asked Nov 5, 2021 at 16:36

2 Answers 2

1

I think you are looking for JSON.parse which will convert string data directly to a JSON object, so long as the string is in valid syntax.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

answered Nov 5, 2021 at 16:48
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of

data-category=${product.category}

do

data-category=${product.category.join(' ')}

You are getting the comma because the default serialization of an array separates elements with a comma.

answered Nov 5, 2021 at 16:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.