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

8
  • I may be missing something there, but aren't the items just strings (not objects)? Commented Mar 1, 2016 at 2:11
  • Negative, the item is everything thats enclosed by the curly brackets in my first code snipped. So it has the property "id", "title", "handle", etc. Commented Mar 1, 2016 at 2:12
  • Now I see it, my bad Commented Mar 1, 2016 at 2:14
  • I'm guessing you're looking for a product with the "product_type" property equals Computer or Keyboard right? Commented Mar 1, 2016 at 2:18
  • That is correct. I've tried removing the "includes and just gone for the pure "if(item[property] == "Computer" | | item[property] == "Keyboard")"... but, while my code runs and compiles, it prints nothing when I test with console.log Commented Mar 1, 2016 at 2:20

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Great. I'm more than glad to help
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/

3 Comments

thats how I've been debugging so far. I'll give this a whirl and let you know what turns up!
It compiles and runs fine, but when I stick a console.log in the 'if' statement, nothing prints. the program just runs to completion
Can you turn your code into a jsfiddle, jsbin, or SO MCVE? I'm sure we can fix it fast if we see the whole thing in action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.