1

According to these answers, I should be able to search object by the multiple values.

How to find a value in a multidimensional object/array in Javascript?

Javascript: How to filter object array based on attributes?

Search multi-dimensional array JavaScript

Unfortunately filter() doesn't return anything. Same goes for jQuery's grep().

Google 5 pages, still can't find the solution.

Please help. Thanks

https://jsfiddle.net/4z90rvk3/

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 price = itemPrices.filter(function(item) { return item.typeid == 1 && item.catid == 2; });

    $('body').append(price);
Community
  • 1
  • 1
Alex G
  • 3,048
  • 10
  • 39
  • 78

3 Answers3

1

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:

Array.prototype.filter() - MDN

How does Array.prototype.slice.call work? - Stack Overflow

Community
  • 1
  • 1
iplus26
  • 2,518
  • 15
  • 26
  • Your jsfiddle = mine; – Alex G Dec 07 '15 at 05:49
  • Sorry I don't understand you. Your object is not changed and a new array is created... Could you give me an example to make it clear? – iplus26 Dec 07 '15 at 05:57
  • Do I need to match `length: 12,` with the actual number of items in object? – Alex G Dec 07 '15 at 06:00
  • @AlexG Yea... If you use the first way, you have to know the 'length' of the object to make it array-like. Or if you try the second one, you don't need to know the length. – iplus26 Dec 07 '15 at 06:02
0

This might work. Filter function works on arrays only.

 var itemPrices = [
            { 'catid': '1', 'typeid': '1', 'price': '1000' },
            { 'catid': '1', 'typeid': '2', 'price': '1000' },
            { 'catid': '1', 'typeid': '3', 'price': '1100' },
            { 'catid': '2', 'typeid': '1', 'price': '1000' },
            { 'catid': '2', 'typeid': '2', 'price': '1000' },
            { 'catid': '2', 'typeid': '3', 'price': '1100' },
            { 'catid': '3', 'typeid': '1', 'price': '1200' },
            { 'catid': '3', 'typeid': '2', 'price': '1200' },
            { 'catid': '3', 'typeid': '3', 'price': '1300' },
            { 'catid': '4', 'typeid': '1', 'price': '1200' },
            { 'catid': '4', 'typeid': '2', 'price': '1200' },
            { 'catid': '4', 'typeid': '3', 'price': '1300' },
        ];

  var price = itemPrices.filter(function(item) { return item["typeid"] == 1 && item["catid"] == 2; });
FurkanO
  • 7,100
  • 5
  • 25
  • 36
0

I know its overkill but I like to do it this way suppose your object has two property's eg. type and title

var searchCategory = $('body .category-selector').val().toLowerCase().trim();
    var searchText = $('body #search-text').val().toLowerCase().trim();

    if (!searchText) {
        searchText = false;
    }
    if (searchCategory === 'all') {
        searchCategory = false;
    }

    var result = gamesData;

    if (searchText !== false) {
        result = result.filter(function(singleGame) {
            var current_title = singleGame.title.toLowerCase().trim();
            return current_title.indexOf(searchText) === -1 ? false : true;
        });
    }

    if (searchCategory !== false) {
        result = result.filter(function(singleGame) {
            var current_category = singleGame.type.toLowerCase().trim();

            return current_category == searchCategory;
        });
    }

    return result;
coder618
  • 848
  • 1
  • 9
  • 12