Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript map array then compare keys if statement

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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])
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading