Nalluri
Nalluri

Reputation: 101

Search for a value in Object Array using Javascript

I have an Object array. I need to apply contains filter for every property (it has to search for that keyword and return the object if any one of the properties contains entered value). Please let me know how to do contains search using Jquery/Javascript.

Example:

var itemPrices = [
    { 'custName': 'Mike', 'custid': '1'},
    { 'custName': 'secondMike', 'custid': '2' },
    { 'custName': 'Ben', 'custid': '3' },
    { 'custName': 'dan', 'custid': '4' }
];

So, from the above array, If I search for Mike it has to return the 2 records which contain mike in the custName or if I search for 1 it has to return the customer with id 1. So it has to search each and every property in each object and return the matched object.

It is like more of a general search in the object array.

Thanks

Upvotes: 1

Views: 1413

Answers (3)

Phillip Thomas
Phillip Thomas

Reputation: 1479

This can be done by iterating the array of objects, then the properties of each object like so:

var matches = [];
var searchString = 'Mike';
var itemPrices = [
    { 'custName': 'Mike', 'custid': '1'},
    { 'custName': 'secondMike', 'custid': '2' },
    { 'custName': 'Ben', 'custid': '3' },
    { 'custName': 'dan', 'custid': '4' }
];

for (var i = 0; i < itemPrices.length; i++) {
    for(var prop in itemPrices[i]) {
        if (itemPrices[i][prop].includes(searchString)) {
            matches.push(itemPrices[i]);
            break;
        }
    }
}

console.log(matches);

As a function, because that is a more reusable solution:

function searchObjectArrayProperties(searchString, arrayToSearch) {
  var matches = [];
  for (var i = 0; i < arrayToSearch.length; i++) {
    for(var prop in arrayToSearch[i]) {
      if (arrayToSearch[i][prop].includes(searchString)) {
        matches.push(arrayToSearch[i]);
        break;
      }
    }
  }

  return matches;
}

var itemPrices = [
  { 'custName': 'Mike', 'custid': '1'},
  { 'custName': 'secondMike', 'custid': '2' },
  { 'custName': 'Ben', 'custid': '3' },
  { 'custName': 'dan', 'custid': '4' }
];

// Then use the function like:
console.log(searchObjectArrayProperties('Mike', itemPrices));

Upvotes: 1

Milesman34
Milesman34

Reputation: 307

const filterPrices = (arr, value) => arr
                                .filter(e => Object.values(e)
                                    .some(f => f.includes(value)));

This function should work. It takes the array of objects and a value to search for. It filters the array, checking the values (Object.values) of the object, then checking if any value (.some) of the array of values includes the value searched for.

This assumes that you have support for String.prototype.includes andObject.values in the environment you are developing for.

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386786

You could filter the array by checking all properties.

If you have properties which are not strings, you need to convert to string before using String#indexOf.

function search(array, value) {
    value = value.toString().toLowerCase();
    return array.filter(function (o) {
        return Object.keys(o).some(function (k) {
            return o[k].toString().toLowerCase().indexOf(value) !== -1;
        });
    });
}

var itemPrices = [{ custName: 'Mike', custid: '1'}, { custName: 'secondMike', custid: '2' }, { custName: 'Ben', custid: '3' }, { custName: 'dan', custid: '4' }];

console.log(search(itemPrices, 'Mike'));
console.log(search(itemPrices, 2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 4

Related Questions