Ask coding questions

← Back to all posts
Trouble with filtering array based on data from two other arrays
cburrus (1)


I'm really struggling to figure out how to solve this filtering problem.

I have an array of exercises that are each objects. I am fetching the data and putting it in a let called highResistanceTotalBodyPower_API on ./script row 4.

Each exercise objects has data such as .restrictions and .equip

I also have an example fitness client ( ./script row 15-17) which contains a client's restrictions and available equipment (equip).

The problem:

I need to filter the array of exercises to match what is available to the client's restrictions and equipment (equip). For instance, if a client does NOT have a barbell then all of the exercises that require barbells should be filtered out. In addition, if a client has a lowBack restriction then all exercises that do NOT contain the restriction should be filtered out.

I gave it a shot in ./script row 21-54; however, it's clearly not working and I'm sure my code is bloated. Any help would be AMAZING as I've been working of this for over a month.

Thanks in advance!!


DynamicSquid (4914)

On line 34:

let convertRestrictionStringToArray =
  highResistanceTotalBodyPower_API[i].restrictions.split(" , ")

Shouldn't the index be j instead of i?

cburrus (1)

Just tried that and it output more than the number of exercises in the array. @DynamicSquid

DynamicSquid (4914)

@cburrus That's weird...

Anyway, I think the problem you're facing is with the for loop order. I'm having a hard time understanding your variable names 😅 (and I'm not really good with JS), but I think your function should look like this:

function filterProgression() {
	for (let j = 0; j < highResistanceTotalBodyPower_API.length; j++)
		let convertRestrictionStringToArray = highResistanceTotalBodyPower_API[j].restrictions.split(" , ");

		for (let k = 0; k < convertRestrictionStringToArray.length; k++)
			let ok = true;

			for (let i = 0; i < restrictions.length; i++)
				if (convertRestrictionStringToArray[k] == restrictions[i])
					ok = false;
			if (ok)


Also, quick question, this is a list of exercises right:


Then what's this:

let convertRestrictionStringToArray = highResistanceTotalBodyPower_API[i]

// what does 'data' store?
let data = convertRestrictionStringToArray[k]

So you convert an exercise to a string array? And what does the string array store?

cburrus (1)


I'll try this. Thanks!

Correct, highResistanceTotalBodyPower_API
is the list of exercises