I am implementing some jquery code for filtering, which initially takes data from a json file:
function getProducts() {
$.get("products.json", (products) => {
...;
})
However, instead of fetching a json file, I'd like to use a json-like array and use that instead of the json file. This data would come from the Django backend, which would pass the data onto the html template.
Something like this:
var products = {{jsonData}}
which would then be this:
var products = [
{
"id": 1,
"category": "shirts",
"name": "Fantasy T-shirt",
"rating": 4,
},
{
"id": 2,
"category": "hoodies",
"name": "Wolf Hoodie",
"rating": 5,
},
]
How could I modify the jquery code to accept that?
Thanks!
json-like arraycoming from ? not clear what are trying to do ?products = JSON.parse(products)inside that function. It will work if the json string is valid. See JSON.parse()$.eachfunction in your$.getand get all the data from yourarrayand then populate it where the data needs to appear.