Removing Empty Strings From Array

I am trying to replicate a recipe app which looks like this:

snip

As you can see I need to display the ingredients in a listed form. I have managed to save all the ingredients in an array called items ( codePen Link below) .

However, when I am getting the data from the API for the recipes (eg. I used: Cake) the ingredient property list (" strIngredient ") contains a lot of empty strings which are getting stored in the items array as well. To remove the unwanted empty strings, I used two methods as shown :

THE TWO METHODS USED FOR FILTERING:

Unfortunately, neither of the methods is working.

CONSOLE BEFORE AND AFTER FILTERING ARRAY:

Where am I going wrong? I checked the syntax for both the For-In loop as well as the .filter() method but was unable to figure out the mistake.

CodePen Link

Regex is good to use here. For example you can allow only strings that contain letters

arr.filter(str => /\w+/.test(str))
2 Likes

thanks ! it worked. However, can you tell me where I am making a mistake while using the for-in loop and filter method ? Want to make sure that my basic understanding is right.

1 Like

Since you’re checking strict equality using !==, I imagine you might not have the characters correct. They could be tabs while you’re checking for spaces, or something like that.

It would be much easier to help if you could share the actual text of the array you’re getting from the API rather than a screenshot.

CodePen

will this codepen link do? The api is right on top and the code is console logging the object data that is being received.

Thanks! That helps a lot.

It looks like you build the items array up yourself, and then you’re trying to go back and delete the unneeded items. I think you should try instead to not push them in the first place. (Hint: you can safely ignore them if the ingredient value is an empty string)

1 Like

Thanks for the help! Will try doing that :+1:

Good day please I’m totally new here how do i start from the basics

start at the begging of the curriculum at freecodecamp.org

1 Like

Hello! Sorry about the late reply. As someone mentioned, try doing the tutorial on this website. There are a lot of very good courses even on Utube. Just remember: Keep on getting your hands on the keyboard and code stuff yourself. How much ever conceptual videos you cover, you will only truly understand the basics when trying it out yourself. Best of Luck! :blush:

By the way, were you able to get a working solution from my suggestion? If you’d like I could show you how I would have solved the problem.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.