Home > Mobile >  array.find(name) returns TypeError name is not a function - Javascript
array.find(name) returns TypeError name is not a function - Javascript

Time:04-16

I've looked as similar questions online but couldn't find a solution that worked for me.

Problem

I have the following 2 constants:

const short_names = { aa: 'AppleAid', bb: 'BananaBoat', cc: 'CloudCut'};
const long_names = ['AppleAid', 'BananaBoat', 'CloudCut'];

And the following custom function find_name(name)

function find_name(name) {
    if (name.length === 2) return (short_names[name] || 'na');
    else return (long_names.find(name) || 'na');
}

When running

console.log(find_name('BananaBoat'))

Locally and in an online compiler, I find the same TypeError:

node /tmp/o2G8YS3Ds1.js
/tmp/o2G8YS3Ds1.js:6
    else return (long_names.find(name) || 'na');
                            ^

TypeError: BananaBoat is not a function
    at Array.find (<anonymous>)
    at find_name (/tmp/o2G8YS3Ds1.js:6:29)
    at Object.<anonymous> (/tmp/o2G8YS3Ds1.js:9:13)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)

Failed Attempts

  • I tried changing my print statement to this:
    console.log(find_name('BananaBoat')[0])
    
  • I also tried changing my function to use the ternary notation:
    function find_name(name) {
        return (name.length === 2) ? (short_names[name] || 'na') : (long_names.find(name) || 'na');
    }
    
  • I've tried implementing this Answer with using filter:
    function find_name(name) {
         if (name.length === 2) return (short_names[name] || 'na');
         else return (long_names.filter(function (x) {
              return x === e
         })[0]|| 'na');
    }
    
    But then I get a e is not defined error.

Edit

The goal is to have the function return the name of the matched element if there is one and if not then just na.

I'm sure this is a straightforward solution to some simple mistake I've made, but because I'm not an expert in Javascript I'm unable to find the solution.

CodePudding user response:

This is because find takes a function as its argument—not a string. If you wanted to use it here, you'd need to supply a function, such as (s) => s === 'BananaBoat', as the argument. The function you provide should return true when it encounters the element you're looking for:

function find_name(name) {
    if (name.length === 2) return (short_names[name] || 'na');
    else return (long_names.find((s) => s === 'BananaBoat') !== -1 || 'na');
}

Instead, I would suggest using indexOf. It returns the first index of the string you pass in, or -1 if the element isn't found:

function find_name(name) {
    if (name.length === 2) return (short_names[name] || 'na');
    else return (long_names.indexOf(name) !== -1 && name || 'na');
}

You could also consider this, which might be a bit clearer:

function find_name(name) {
    if (name.length === 2) return (short_names[name] || 'na');
    if (long_names.indexOf(name) !== -1) return name;
    return 'na';
}
  • Related