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

How to display values from array in JS?

I want to display all values from array categories, but I always got [object object] or undefined.
Here is code in last lines if statement, there is that problem in part between bracket [] when I want to call categories and type of categories is Object:

  const urlKeys = [...new URL(window.location.href).searchParams.keys()];

  let categories = [];

  let count = urlKeys.length;

  for (let i = 0; i < count - 1; i++) {
    categories.push({ type: paramsFromUrl.get('category' + i) })
  }  //Here I push elements in categories

  if (urlKeys[0] === 'allCategories') {
    document.getElementById('notice').innerText = "All categories included: [" + categories + "]";
    return viewsjs.createRestaurantCards(filterjs.findRestaurantsByCategory(await restaurantsjs.loadRestaurantsJSON(), categories));
  } //Here is problem

>Solution :

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

categories is an array of objects. If you want to convert that to a string, you need to do JSON.stringify

"All categories included: " + JSON.stringify(categories);

If you only want to display the type and not the whole object, then:

"All categories included: [" + categories.map(obj => obj.type) + "]";
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