question

newuser-4bd634ba-2a53-4df1-a732-f59a52846ffc avatar image
newuser-4bd634ba-2a53-4df1-a732-f59a52846ffc asked ·

Need help using slot value for an array filter

I have a 2 dim array in a quotes.js file. It has the type of quote and the quote itself like below:

module.exports = [

[`Happiness`, `For every minute you are angry you lose sixty seconds of happiness.`],

etc.

In my code below I am trying to us the slot as a variable to use on a filter statement. The code as I have does not work. Below is the code for the intent:

var qtype = handlerInput.requestEnvelope.request.intent.slots.type.value;

var col1 = quotes.filter(function(x) {
//return x[0] === 'Happiness'; // WORKS
return x[0] === `${qtype}`; //NOT WORKING

Am I missing some type of formatting? This first part creates a new array col1 that should only get the list of quotes that match the slot value for that type. Later I select the random quote itself from col1 array.

Thanks for any help in advance.

David

alexa skills kitskillintentsjavascript
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rokas avatar image
Rokas answered ·

I have one more idea what's wrong. Slot values come lowercased, so try lower casing 'Happiness' -> 'happiness' in that object and just to be save on qType call 'tolLowerCase()' as well:

const qType = handlerInput.requestEnvelope.request.intent.slots.type.value.toLowerCase();
1 comment
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks for the idea with the lower case. I changed the Happiness in the quotes.js array file to lower case and it seems to be working now. I didnt know it was so case sensitive. Now i know all slots come in lower case. Spent hours on this and it was something so simple.

Thanks again for the help.

1 Like 1 ·
Rokas avatar image
Rokas answered ·

I would recommend to approach this a bit different. I would suggest you to store data in an object like this:

module.exports ={
  `Happiness`: [
     `For every minute you are angry you lose sixty seconds of happiness.`, 
     `Another random string`
  ],
  `Some other slot`: [`some other strings`]
}

Now when you have structure like this you can get an array of quotes by slot value like this:

const qType = handlerInput.requestEnvelope.request.intent.slots.type.value;
let quotesArray = quotes[qType]

I've spotted and bolded word `type`, because this has to be your slot name. Is you slot really called `type`? I would suggest to log `qType` to check if you are retrieving slot value correctly, because this is might be the case, why your existing code doesn't work.

There is also a helper method available in ask-sdk called getSlotValue, which helps to get slot type easier.

1 comment
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

The slot name is type. I changed the array as you suggested and the code modifications. The slot value is correct from the test console and also tested in Visual Studio Code. When i put code like this: const speakOutput = `The Type of Quote is ${qtype}`; it shows correct. If I try in code like this: let col1 = quotes2[qtype] it does not work. It will work if used like this: let col1 = quotes2['Happiness']. Still not picking up the slot value.

Thanks

0 Likes 0 ·