[SOLVED] Javascript map array then compare keys if statement

Issue

I have an array of the alphabet and 2 sets of values, Im trying to check if a value is between the 2 given values then print out the letter. My code:

var alphabet_num = {
"alphabet": ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"],
"start": ["0", "150", "300", "450", "600", "750", "900", "1050", "1200", "1350", "1500", "1650", "1800", "1950", "2100", "2250"],
"end": ["149", "299", "449", "599", "749", "899", "1049", "1199", "1349", "1499", "1649", "1799", "1949", "2099", "2249", "2399"]
};

var xValue = 76;
var result = alphabet_num.alphabet.map(
(k, i) => [
    k, alphabet_num.start[i], alphabet_num.end[i]

    if (xValue >= alphabet_num.start[i] && xValue <= alphabet_num.end[i]) {
        //a
    }
]
);

Solution

If you are looking for a specific value, you don’t need the map function, you could solve it with find.

const result = alphabet_num.alphabet.find(
  (_letter, i) =>
    parseInt(alphabet_num.start[i]) <= xValue &&
    xValue <= parseInt(alphabet_num.end[i])
);

Answered By – davidies

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *