0

I have a html table I an trying to create a function for. I will loop over all of the column names and if they contain a word that is in my array, I will turn that columns value into a formatted link accordingly.

function createLink(field, val){

    // Create a mapping of our data
    var output = {
        'ntid': 'https://example.com/profile/'+val,
        'email': 'mailTo:' + val
    };

    // Test and return values
    // pseudo code

    if(field matches a key partially in output){
        return '<a href="'+output[value]+'" target="_blank">'+[val]+'</a>';
    }else{
        return 'No Matches';
    }

}

 // Examples
 createLink('NTID', 'bob'); // '<a href="https://example.com/profile/bob" target="_blank">bob</a>';
 createLink('Sup NTID', 'bob'); // '<a href="https://example.com/profile/bob" target="_blank">bob</a>';
 createLink('Email', '[email protected]'); // '<a href="mailTo:[email protected]" target="_blank">[email protected]</a>';
 createLink('Sup Email', '[email protected]'); // '<a href="mailTo:[email protected]" target="_blank">[email protected]</a>';

How could I go about testing the value to see if it has a partial match in my output array and then returning its formatted link?

Since these column names are dynamic and could change any time, I just want to test for partial words rather than the exact string.

If there is no match, I could just return a placeholder value such as "No Match".

6
  • You could use indexOf to check if the value is in the object. Ex. if(output.indexOf(string) === -1) {//No match} else {//Match} NOTE. This will not work if you are checking with the key-values of your object. Commented Feb 13, 2017 at 19:22
  • @9focuspoints - This would tell me if its in there but how would I then would I access it directly in order to get the value? Commented Feb 13, 2017 at 19:35
  • Use regex with i flag new RegExp(valueToCheck.replace(/[.?*]/g,"\$1"), "i") Commented Feb 13, 2017 at 19:36
  • Post the real output array with some examples! Commented Feb 13, 2017 at 19:36
  • @ibrahimmahrir - Updated the post with some examples Commented Feb 13, 2017 at 19:52

1 Answer 1

1

Iterate Object.keys and return if a key is in the "field" string:

function createLink(field, val) {

    var output = {
        'ntid': 'https://example.com/profile/' + val,
        'email': 'mailTo:' + val
    };

    for (var key of Object.keys(output))
        if (field.toLowerCase().includes(key))
            return `<a href="${output[key]}" target="_blank">${val}</a>`;

    return 'No Matches';
}

// Examples
console.log(createLink('NTID', 'bob'));
console.log(createLink('Sup Email', '[email protected]'));

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

1 Comment

This worked perfectly for Firefox but having issues with IE due to it not liking key of or includes(). Any chance you could show an example of an all around compatible version?

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.