DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Array Search Prototype

Snippets Manager user avatar by
Snippets Manager
·
Mar. 05, 07 · · Code Snippet
Like (1)
Save
Tweet
2.21K Views

Join the DZone community and get the full member experience.

Join For Free
This prototype extends the Array object to allow for searches
within the Array.   It will return false if nothing is found.  If
item(s) are found you'll get an array of indexes back which matched
your search request. It accepts strings, numbers, and regular expressions as search
criteria.  35 is different than '35' and vice-versa.   

// Examples
var test=[1,58,'blue','baby','boy','cat',35,'35',18,18,104]
result1=test.find(35);    //returns 6
result2=test.find(/^b/i); //returns 2,3,4
result3=test.find('35');  //returns 7
result4=test.find(18);    // returns 8,9
result5=test.find('zebra'); //returns false



Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i
Prototype Data structure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why I'm Choosing Pulumi Over Terraform
  • 10 Books Every Senior Engineer Should Read
  • Debugging Deadlocks and Race Conditions
  • How to Hash, Salt, and Verify Passwords in NodeJS, Python, Golang, and Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo