2

I have the following information:

  1. The city name, in this case Littelside. This is stored in a variable var city

  2. I have an array of {} objects. Each contains address, city, name, state and zip-code

[{"address":"07288 Albertha Station",**"city":"Littelside"**,"created_at":"2011-05-25T19:24:51Z","id":1,"name":"Mr. Emmitt Emmerich","state":"Missouri","updated_at":"2011-05-25T19:24:51Z","zip":"75475-9938"},{NEXT OBJECT WITH ADDRESS CITY ETC}]

How do you use the var city as a search term to parse the array, and return address, city, state, name and zip of the array with the matching city?

Thanks!

5 Answers 5

2

The function you're looking for is $.grep().

var city='Littelside';

var cucc=$.grep(your_array, function (a) {
    return a.city==search;
})[0];

$.grep will still return an array, with all the elements inside that satisify the filter function. Here I've use [0] to get the first result, which is fine in case you're sure that there will be one result. So cucc will have the whole object you're looking for.

jsFiddle Demo

Sign up to request clarification or add additional context in comments.

Comments

1
var myarr  =  [{"address":"07288 Albertha Station","city":"Littelside","created_at":"2011-05-25T19:24:51Z","id":1,"name":"Mr. Emmitt Emmerich","state":"Missouri","updated_at":"2011-05-25T19:24:51Z","zip":"75475-9938"}];
var city = 'Littelside';
$.each(myarr,function(key,value){
    if(value['city']== city){
        alert('hello');
    }
})

here's a working demo

2 Comments

books no... who needs books when you have the internet... I try not to write any native js try to use jquery as much as possible you can find documentation here its been truly more helpful than any book i've read
@JZ Javascript: The Good Parts from Douglas Crockford is a must :).
1

A function I use.

function arrayIndexOfProp(arr, prop, obj) {
        var i = arr.length;
        while (i--) {
            if (arr[i] && arr[i][prop] === obj) {
                return i;
            }
        }
        return -1;
    }

var index = arrayIndexOfProp(theArray, "city","Littelside");
// do stuff with theArray[index]

1 Comment

thanks! a tiny bit of cleverness to save a few chars over a for loop :)
1

Array.prototype.reduce as in

Array.prototype.reduce(
    function (prev, current) {
      return prev || current.city === "Littelside" ? current : null;
    })

2 Comments

Browser support is not really there AFAIK, but still +1.
@bazmegakap, You are correct that it is not yet widely supported. The compatibility section (link follows) does have code that you can use to back-port it onto older browsers. developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
-1

Loop over the array until you find one where the_array[i].city matches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.