Search by multiple values in object

0
900

filter() function works only for array. So you need to make itemPrices an array. There are two ways to transform the object to an array:

The first one is, if you can change the object, add a length property to it and then use Array.prototype.slice.call(itemPrices) method to make it an array, check out the updated jsfiddle.

The second one is easier to understand:

var itemPrices = {
		'1':  { 'catid': '1', 'typeid': '1', 'price': '1000' },
		'2':  { 'catid': '1', 'typeid': '2', 'price': '1000' },
		'3':  { 'catid': '1', 'typeid': '3', 'price': '1100' },
		'4':  { 'catid': '2', 'typeid': '1', 'price': '1000' },
		'5':  { 'catid': '2', 'typeid': '2', 'price': '1000' },
		'6':  { 'catid': '2', 'typeid': '3', 'price': '1100' },
		'7':  { 'catid': '3', 'typeid': '1', 'price': '1200' },
		'8':  { 'catid': '3', 'typeid': '2', 'price': '1200' },
		'9':  { 'catid': '3', 'typeid': '3', 'price': '1300' },
		'10':  { 'catid': '4', 'typeid': '1', 'price': '1200' },
		'11':  { 'catid': '4', 'typeid': '2', 'price': '1200' },
		'12':  { 'catid': '4', 'typeid': '3', 'price': '1300' },
	};

var arr = [];

for (var item in itemPrices) {
  if (itemPrices.hasOwnProperty(item)) {
        arr.push(itemPrices[item])
  }
}

var price = arr.filter(function(item) { return item.typeid == 1 && item.catid == 2; });
	
console.log(price);

Reference Link – https://stackoverflow.com/questions/34127030/search-by-multiple-values-in-object

LEAVE A REPLY

Please enter your comment!
Please enter your name here