2

I'm writing a program that takes a store inventory and searches for specific items in that inventory, pushing those items into an array. The inventory is all one object, and each object within this object is an item of the inventory. The items themselves have no keys- they're simply object literals. Therefore I'm stuck looping through them using fast enumeration (for item in products). Each item looks like this:

{   id: 2759167427,   
    title: 'Practical Silk Bag',   
    handle: 'practical-silk-bag',  
    vendor: 'Legros, Willms and Von',  
    product_type: 'Bag'
}

What I'm trying to do is push the item object to an array if and only if that item is either a keyboard or a computer. To that end I tried employing something like this :

var kbComps = [];

//loop through the inventory, looking for everything that is a keyboard or a computer
for (var key in products) {
    var item = products[key];
    for (var property in item) {
        if (item[property].includes("Computer") ||  item[property].includes("Keyboard")) {
            kbComps.push(item);
        }
    }
}

However I'm getting an error that tells me includes isn't a defined method, meaning the program isn't recognizing item[title] as a string, so now I'm stuck. How would I circumvent this? Any help is appreciated.

Cheers all

David Tamrazov
  • 567
  • 1
  • 5
  • 16

2 Answers2

1

In the first iteration of your loop, you're checking if id contains a string but id is a number therefore .includes fails.

I'm not sure what you're intention is but you might want to only check .includes if the item is a string.

if (typeof item[property] === 'string' && (item[property].includes("Computer") || item[property].includes("Keyboard"))) {

If you throw some console logs in you can see what's going on. https://jsfiddle.net/qexssczd/1/

Matt
  • 5,315
  • 1
  • 30
  • 57
  • thats how I've been debugging so far. I'll give this a whirl and let you know what turns up! – David Tamrazov Mar 01 '16 at 02:21
  • It compiles and runs fine, but when I stick a console.log in the 'if' statement, nothing prints. the program just runs to completion – David Tamrazov Mar 01 '16 at 02:25
  • Can you turn your code into a jsfiddle, jsbin, or SO [MCVE](http://stackoverflow.com/help/mcve)? I'm sure we can fix it fast if we see the whole thing in action. – Matt Mar 01 '16 at 02:26
1

UPDATED

I changed the implementation to loop over an object and not an array. I think this is what you are looking for.

Here is a working jsBin

May be this is a little simpler and I'm sure it would work for you

// Products base data
var productsData = {
    a: {
        id: 2759167427,
        title: 'Practical Silk Bag',
        handle: 'practical-silk-bag',
        vendor: 'Legros, Willms and Von',
        product_type: 'Bag',
    },
    b: {
        id: 2759167417,
        title: 'Practical Silk Bag 2',
        handle: 'practical-silk-bag-2',
        vendor: 'Legros, Willms and Von 2',
        product_type: 'Bag 2',
    },
    c: {
        id: 2759167417,
        title: 'Practical Silk Bag 3',
        handle: 'practical-silk-bag-3',
        vendor: 'Legros, Willms and Von 3',
        product_type: 'Computer', // This product must be returned
    },
    d: {
        id: 2759167417,
        title: 'Practical Silk Bag 4',
        handle: 'practical-silk-bag-4',
        vendor: 'Legros, Willms and Von 4',
        product_type: 'Keyboard', // This product must be returned
    }
};


/**
 * Function to find products by any condition
 */
function searchItemsByCondition(products, evaluateCondition) {

    var ans = [];

    for (var item in productsData) {
        // Making sure the object has the property product_type
        if (products[item].hasOwnProperty('product_type')) {
            if (evaluateCondition(products[item])) {
                ans.push(products[item]);
            }
        }
    }
    return ans;
}

function searchByKeyboardOrComputer(product) {
    return (product.product_type === 'Computer') || (product.product_type === 'Keyboard');
}


// Call the function passing the evaluation function to satisfy.
// It should log only the items with 'Keyboard' or 'Computer' product_type
console.log(searchItemsByCondition(productsData, searchByKeyboardOrComputer));

Hope this works for you!

wilsotobianco
  • 1,360
  • 14
  • 19