-2

I want the function to be able to output true or false, if the searchElement exists in the array it should return true or false. I know i could make use of a simple for loop and if-else combination but i want to make use of arrow function.

I know by default 'includes' function exists in js, i am exactly trying to make a function similar to it, that's why i don't want to use it

const somestuff=[1,2,54,23,65,132,76,'A','wth','Health is Wealth'];
function includes(array,searchElement){

    const result=somestuff.filter(arrayElement=> arrayElement===searchElement)
    if(arrayElement===searchElement){
        console.log('true');
    }
    else{
        console.log('false');
    }
}

includes(somestuff,65);

This line alone provides me with the arrayElement, I want it to return true or false, that's why i tried the if else statement but i don't know how to include that code-block in the arrow function line, and i think === should have returned true or false instead of the number itself, plz let me know what i am doing wrong, thanks

const result=somestuff.filter(arrayElement=> arrayElement===searchElement)
  • 1
    filter method is for filtering the array, try to use some https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some or findIndex method, compare with -1 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex – rakesh Jul 29 '21 at 08:42

5 Answers5

1

That can be implemented with find function rather than filter.

Live demo: https://replit.com/@kallefrombosnia/PalatableStingySphere#index.js

// Define function
const includes = (array, element) =>{
  return array.find(item => item === element) ? true : false;
}

// Log output in console
console.log(includes([1, 2, 3], 4));

const includes = (array, element) =>{
  return array.find(item => item === element) ? true : false;
}

console.log(includes([1, 2, 3], 4));
kalle
  • 144
  • 1
  • 9
  • @kale the find function still returns a number not boolean value – Rana Shaharyar Jul 29 '21 at 08:47
  • That's why we use JS truthy falsy feature. Any found result is truth and it will return true. If the element is not found find will return undefined and that is falsy which will return false. – kalle Jul 29 '21 at 08:50
1

Another solution for this problem is to use some method from Array prototype.

const includes = (array, pattern) => {
    const comp_function = (element) => element === pattern
    return array.some(comp_function)
}

k = [1, 2, 3, 'a', 'b', 'c']

includes(k, 1) // true
includes(k, 'a') // true
includes(k, '1') // false
includes(k, 'd') // false
Julien Sorin
  • 703
  • 2
  • 12
0

why do you have to implement that "includes",

we have Array.prototype.includes() specification so we can do something like


const array1 = [1, 2, 3];

array1.includes(2) // return true

And learn more about the spec here

varaprasadh
  • 488
  • 5
  • 13
0

You can use Array.prototype.find(). I explain filter is to remove from and array some element which have the same pattern, however you can use map() or find() is the ES6 feature that makes our life easier. This demo will explain to you

// map()

const result=somestuff.find(arrayElement=>  { 
if(arrayElement===searchElement){
        console.log('true');
return arrayElement
    }
    else{
        console.log('false');
    })
})

// find()

const result=somestuff.find(arrayElement=> arrayElement===searchElement)

Finally, we got this

const somestuff=[1,2,54,23,65,132,76,'A','wth','Health is Wealth'];
function includes(array,searchElement){

   const result=somestuff.find(arrayElement=> arrayElement===searchElement)
   return result
}

includes(somestuff,65);
Anas Ben Yaiche
  • 225
  • 3
  • 7
0

this is how i solved the problem using your help guys

i needed the index of where the variable searchElement exists and whether it does exist or not.

const exists = (element) => element === searchElement;
    console.log(' exists at index: '+somestuff.findIndex(exists) +' therefore: '+somestuff.some(exists));

meaning whichever value satisfies the requirement gets stored in exists variable and now i can apply array methods on them such as findIndex() and some()

findIndex() tells about index of value and some() will return true even if one value satisfies the requirement

This is what i originally wanted to do though:

const somestuff=[1,2,54,23,65,132,76,'A','wth','Health is Wealth'];
const searchElement=54;
const result=somestuff.find(arrayElement=>  { 
    
    if(arrayElement===searchElement){
        console.log('true '+arrayElement);
    console.log(somestuff.indexOf(searchElement));}
    });