Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching an Array of JSON objects

I'm not sure how to accomplish this following given this array of JSON objects:

var stuff = [
  {
    'Address' : '123 Street',
    'Name'    : 'From'
  },
  {
    'Address' : '456 Avenue',
    'Name'    : 'To'
  }
]

So what I would like to be able to do is query this array of objects based on one of the properties, in this case 'Name', and return the entire object that matches the query.

Is there anyway to do this with jquery or just regular javascript?

For example I'd like to return the whole object where Name === 'From'

like image 353
Collin Estes Avatar asked Mar 23 '26 11:03

Collin Estes


1 Answers

function findStuff(jsonobject, propertyToFind, valueToFind)
{
    for (var i = 0; i < jsonobject.length; i++) {
        if (jsonobject[i][propertyToFind] === valueToFind)
           return jsonobject[i];
    }
    return null;
}
like image 59
ricosrealm Avatar answered Mar 25 '26 02:03

ricosrealm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!