javascript find element in array – The find() method executes a function for each array element. using arr.includes(obj);, Using indexOf() and Using find() Examples.
javascript find element in array
Best way to find if an item is in a JavaScript array using includes() methods.
using jQuery:
$.inArray(5 + 5, [ "8", "9", "10", 10 + "" ]);
don’t miss : Javascript Find Object In Array By Property Value
Using includes()
const allwebsiteLists = ["pakainfo website", 80, "4 infinity know", "w3diy blog", 80];
Then use includes() to check if the string “pakainfo website” exists in the array:
allwebsiteLists.includes("pakainfo website"); allwebsiteLists.includes("pakainfo website", 1); allwebsiteLists.includes(80); allwebsiteLists.includes('80');
don’t miss : Javascript Array Contains
using Using indexOf()
const allwebsiteLists = ["pakainfo website", 80, "4 infinity know", "w3diy blog", 80];
allwebsiteLists.indexOf("w3diy blog"); allwebsiteLists.indexOf("w3 and schools"); allwebsiteLists.indexOf(80) allwebsiteLists.indexOf(80, 2);
Using find()
const allwebsiteLists = ["pakainfo website", 80, "4 infinity know", "w3diy blog"];
allwebsiteLists.find(el => el.length < 13); allwebsiteLists.find((el, idx) => typeof el === "string" && idx === 2);
I hope you get an idea about javascript find element in array.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.