mercredi 14 août 2019

Is there a way to build a type of dynamic array in javascript?

I am building a hockey stick suggestion "app" based on answers to a quiz. In my quiz, I ask 5 questions about features. If a user doesn't care about one of those features, then they can mark that question "I don't care". The features need to match those coming from a Google Sheets API that has a list of all the hockey sticks the user can match.

To suggest the right stick, I am trying to build a complex If statement that checks a user-built object against properties in a JSON API (The API has 18 objects /sticks). The object has 5 properties and gets the values from answers in a form based on user input. I am wanting to check all values against the various objects stored in the objects found in the JSON API. Then when the properties match push that object to a different array. The trick is that a user can answer "I don't care" to any of the 5 questions that build the object. (I have tried leaving it blank if a user doesn't care but what I tried didn't work) So if a user cares about answering questions 1 2 3 and 5 but marks question 4 - "I don't care" I would need still the other 4 values to match of the object to match those in the API and then push any object that has all matching values.

I have tried various if statements including a very large one with all 5 questions. That seems to be the closest. However, if a user doesn't care about a feature that what makes this very hard.

So the user object that gets built with the answers from the quiz is :

var user = {
    level: '',
    grip: '',
    flex: '',
    pattern: '',
    kickPoint: ''
}

Then for each one of those properties, I have an If statement that checks to see what the user answered, and then change the property to the user answer. So for example flex :

if (flex == '75-77 (Light)') {
    user.flex = "Light"; 
} else if (flex == '85 - 87 (Standard)') {
    user.flex = "Regular";
} else if (flex == '95+ (Stiff)') {
    user.flex = "Stiff"; 
} else {
    user.flex = "I don't care"; 
}

Then once all properties have something, my if statement that has gotten close is (for properties like flex, in my google sheet I have it say Light, Regular or Stiff depending on what the stick is available in. Hence why there are includes ):

for (var j = 0; j < data.user.length; j++) {

    if ( user.level == data.user[j].level && 
         user.grip == data.user[j].grip && 
         data.user[j].pattern.includes(user.pattern) && 
         data.user[j].flex.includes(user.flex) && 
         user.kickPoint ==  data.user[j].kickpoint
      ) {

             userStick.push(data.user[j]); 

         }             

} 

So far I get mixed results and I am not sure what else to do. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire