简体   繁体   中英

Efficient Searching in jQuery

I want to be able to pass an array (even an array of jQuery objects) and do a simple search based on a single data member.

It'd take an array to search, a string for the data member, and the value to search for.

This is obviously easy to write myself, but I was curious if jQuery had anything built in to do this already?

Thanks SO!

var arr = [
    {key1 : 'value 1-1', key2    : 'value 2-1'},
    {key1 : 'value 1-2', key2NOT : 'value 2-2'},
    {key1 : 'value 1-3', key2    : 'value 2-3'},
    {key1 : 'value 1-4', key2NOT : 'value 2-4'}
],
    data_member = 'key2',
    output      = {};

$.each(arr, function (index, obj) {
    if (data_member in obj) {
        output[index] = obj[data_member];
    }
});

Here is a jsfiddle: http://jsfiddle.net/jasper/PupuZ/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM