How to Search a JavaScript Object Array

Posted on
By Addam Driver

Searching a javaScript object can be a little tricky. While working on a project I found the need to search a dataset and do something useful with the findings. So my team and I came up with this nifty little script to allow an object to be searched by single values, multiple values and even wild cards. It’s fast and extremely easy to use. Check it out!

You can DOWNLOAD the script here.

USAGE:
Pass a javaScript object to the function findInObject like this:

findInObject(myObject, {fieldName: fieldValue}, firstMatchOnly)

The first param is the actual object to search. The second is the field and value you are looking for. The third one is a true/false to only return the first match.

You can also look for multiple values like this:

findInObject(myObject, [{firstName: "Adddam" }, {lastName: "Driver" }, {city: "Los Angeles" }])

You can also look for wild cards by using the star ( * ):

findInObject(myObject, [{firstName: "Adddam" }, {lastName: "Driver" }, {city: " * " }])

Kinda Neat eh!? Give it a try and let me know what you think!

Here is the script to DOWNLOAD.

Tags: , , , , , , , ,

Comments

  1. GeorgeMcK says:

    Wow. This sounds great. Can’t wait to try it out.

Leave a Reply to GeorgeMcK Cancel reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CSS Methods

HTML Goodies

JavaScript

mustache

node.js

The HTML Writers Guild IWA