Check Element Existence in Arrays (2)
image author: https://www.google.com/imgres?imgurl=https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--hxSKXT_j--%2Fc_imagga_scale%2Cf_auto%2Cfl_progressive%2Ch_900%2Cq_auto%2Cw_1600%2Fhttp%3A%2F%2Fdevelopmentmatt.com%2Fwp-content%2F

Check Element Existence in Arrays (2)

If you work with a hard code values is easy to work with them because you know where are the values inside the array.

example

// you have an array with names
const names = ['name1','name2','name3',...]


// you can access for example name2 because you know the position
console.log(names.indexOf('name2')) // return 1


// but what happen with an item that doesn't exist?
console.log(names.indexOf('name4')) // return -1 that means that the element doesn't exist


// so you can do something like that to check is an item exist
console.log(names.indexOf('name3') === -1 && true); //return true

fortunately for us there is other much easiest way to do this "includes method"

// you have an array with names
const names = ['name1','name2','name3',...]


// use includes method
console.log(names.includes('name4')) // return false

but what happen with more complex data? (objects)

for the more complex data you cannot use includes and those cases you can use 'some' method.

some method

The some method takes a callback function with its own parameters and iterate over each element of the array.

example

// array of objects
const persons = [
  { name: 'random1', age: 5 },
  { name: 'random2', age: 4 },
  { name: 'random3', age: 56 },
  { name: 'random4', age: 11 },
  { name: 'random5', age: 90 },
];


// use some method
persons.some((element) => element.age === 90); // return true or false

with some method at least one element need to match with the condition, once have a match the iteration stop and returns true or false

Hope that you enjoy this post with love Bernardo Aguayo <3







要查看或添加评论,请登录

Bernardo Aguayo Ortega的更多文章

  • Get Subsets of arrays(4)

    Get Subsets of arrays(4)

    What if we only want a subset of data, no to transform the array we only want the elements that we are interesting on…

  • Perform Actions on all elements arrays(3)

    Perform Actions on all elements arrays(3)

    There are a method incredible powerful call map that allow us to make transform in each element of the array. //we have…

  • Build Flexible Collections with Arrays (1)

    Build Flexible Collections with Arrays (1)

    There are a data structure to save collection of data call it array use [] brackets instead {}, Arrays are a kind of…

  • Merge Objects with Object Spread

    Merge Objects with Object Spread

    If you want to merge various objects you need to spread object's properties in new object and literary since ECMAScript…

  • Object Destructuring (JavaScript)

    Object Destructuring (JavaScript)

    Object destructuring allows us to pull properties from an object and make them into variables, in other words allow us…

  • Use Objects for Managing key-value pairs (js)

    Use Objects for Managing key-value pairs (js)

    We already know the different types of variables in JavaScript so you can see a variable as a box and an object like a…

    2 条评论
  • Shorter functions with arrow functions

    Shorter functions with arrow functions

    With ES6 we have a new form to write functions, that allow us to write shorter functions, we don't need any more the…

    4 条评论
  • How Functions Should Be Named

    How Functions Should Be Named

    A few things to remember when naming functions:Name functions after a verb which clearly conveys what they do Omit…

  • single responsibility functions

    single responsibility functions

    How to make your function only have one responsibility? As the name say it this principle talks about make functions…

  • Better functions with default parameters (javascript)

    Better functions with default parameters (javascript)

    Imagine the you have a function function convertTemperature(celsius , decimalPlaces) { ? const fahrenheit = celsius *…

    4 条评论

社区洞察

其他会员也浏览了