How to make Alexa read a simple dictionary?

Issue

I’m trying to make Alexa pick an item from a dictionary by his name

const Alexa = require('ask-sdk-core');

const garbage = [
    { 'name': 'can', 'color': 'green' },
    { 'name': 'magazine', 'color': 'red' },
    { 'name': 'bottle',  'color': 'yellow'},
    { 'name': 'crap', 'color': 'gray'},
    { 'name': 'apple', 'color': 'brown'}
    ];

and reply with a defined color

const RecycleIntentHandler = {
    handle(handlerInput) {
        const {requestEnvelope, responseBuilder} = handlerInput;
        const {intent} = requestEnvelope.request;
        
        const object = Alexa.getSlotValue(requestEnvelope, 'object');
        
        let res = garbage(el => el.name === object);
        var speechText = (res.color);

        return handlerInput.responseBuilder
            .speak(speechText)
    }
};

(store what the users said in "object", then search object.name in the dictionary and pronounce his colour)

I tried different combination of code but I can’t make it work.
What am I doing wrong?

Solution

Use the find function:

let res = garbage.find(el => el.name === object);

Your constant name gargabe is not a function. It’s a constant.

Answered By – callmemath

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.