简体   繁体   中英

jQuery - Regex on value of each JSON key-value pair array

Suppose you have an array of JSON key-value pairs, such as the following:

var LNsIDs = [
    {2053 : 'Jones'},
    {9862 : 'Roberts'},
    {1567 : 'Collins'},
    {etc}
];

Is there a way to reference only the values of these pairs without dissociating them from their keys? (In other words, without looping through the array and feeding the values into a new array.)

I am performing a regular expression for an autocomplete function that works with a regular array. This part of the code would look like this:

$("#lastname").autocomplete({
    source: function(request, response) {
        var esc = $.ui.autocomplete.escapeRegex(request.term);
        var regex = new RegExp("^" + esc, "i");
        var result = $.grep(LNsIDs, function(item){
            return regex.test(item.label);
        });
        response(result);
    },
    select: ...

This works with a regular array - for example, if the array was the following:

var LNsIDs = ['Jones', 'Roberts', 'Collins', etc]; 

BOTTOM LINE: How can I perform this regular expression on the values of these pairs in such a way that I can then retrieve the key of the selected pair?

It's a bit difficult to determine exactly what you want, but maybe this will put you on the right track. It appears that the primary difficulty is that the objects in your LNsIDs array do not have consistent property names, so you cannot easily reference them. Here is a potential solution:

 var LNsIDs = [ {2053: 'Jones'}, {9862: 'Roberts'}, {1567: 'Collins'}, ]; var regex = /^R/i; var result = $.grep(LNsIDs, function(item) { // Pull out the name of the first property on our object var key = Object.keys(item)[0]; // Use the key to lookup and test the value of that property if (regex.test(item[key])) { // We match, so add two new properties to the object // before returning true: // // 1. The property name (ie 2053) // 2. The value of that property (ie Jones) item.key = key; item.label = item[key]; return true; } // We don't match so we don't bother setting additional // properties return false; }); // Once we have our result, it will just be an array of matches // and each of the objects in it will have a 'key' and a 'label' // property we can look at: for (var i = 0; i < result.length; i++) { alert('Found match with key ' + result[i].key + ' and label ' + result[i].label); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

I was eventually able to achieve this by doing it the "right" way, which is to say, by putting an two separate AJAX calls in the source and select parameters of the autocomplete function, and outputting a traditional JSON array from my data source. Obviously, this would have been the correct way to perform this action all along. Thank you!

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