0

This is a continuation of a question I asked yesterday, where I asked how to determine if the input matches any portion of an array's element ('sun' & 'sunday' = 'sun'). Here was the solution.

var _array = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];

for (var i = 0; i < _array.length; i++) {
    if (_val.indexOf(_array[i]) != -1) {
        $('span').text('it worked');
        return;
     }
}

$('span').text('nothing');  

I just realized however that, if true, I need to match it with the full name value in an associative array. Ex: if you type ('sun','sund','sunda', or 'sunday') it returns 'sunday'. Ideally it would be case insensitive, but I can figure that out on my own.

var _array = {
    "sun" : "sunday",
    "mon" : "monday",
    "tue" : "tuesday",
    "wed" : "wednesday",
    "thu" : "thursday",
    "fri" : "friday",
    "sat" : "saturday"   
};

Here is the second part of the solution. I realize I could just remove the else { $('p').text(''); } in this example but that won't work in my project code.

1
  • Please put all of the code into the question. It makes the question stand on its own. External sites may not be around in the future. Commented Aug 16, 2012 at 1:27

3 Answers 3

1

If i udnerstand from your previous question, if any part of the text input matches an item in the array, then it is considered a matched.

Now it seems that you want to map the short array version to a full word.

If this sounds right, you can just get the short value from the Array, and create a map of short to long versions.

var map = {
   sun: "sunday",
   mon: "monday",
   tue: "tuesday",
   wed: "wednesday",
   thu: "thursday",
   fri: "friday",
   sat: "saturday"
};

Then get the array value, and pass it as the key to the map.

var _array = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];

_val = _val.toLowerCase();

for (var i = 0; i < _array.length; i++) {
    if (_val.indexOf(_array[i]) != -1) {
        $('span').text('it worked');
        alert(map[_array[i]]);  
        return;
     }
}

You also said you want it case insensitive, so I added toLowerCase() to _val.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I used the wrong index in the map. It should be alert(map[_array[i]]); jsfiddle.net/DQPx7/4
Looks good but be sure to put the map and the _array outside the keyup event handler because you don't need to make them on every event.
1
function parseDay(val) {
    val = val.toLowerCase();
    var days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
    for (var i = 0; i < days.length; i++)
        if (days[i].indexOf(val) == 0)
            return days[i];
}

parseDay('sun'); // 'sunday'

Comments

0

It sounds like you need to use .match()

Example: http://jsfiddle.net/t5pP8/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.