Javascript 访问对象对象中的特定对象

Javascript 访问对象对象中的特定对象,javascript,string,loops,object,Javascript,String,Loops,Object,我正在编写一个程序,它获取一个商店库存,并搜索该库存中的特定项目,将这些项目推送到一个数组中。库存都是一个对象,此对象中的每个对象都是库存的一个项目。这些项本身没有键——它们只是对象文字。因此,我一直在使用快速枚举(针对产品中的项)遍历它们。每个项目如下所示: { id: 2759167427, title: 'Practical Silk Bag', handle: 'practical-silk-bag', vendor: 'Legros, Wil

我正在编写一个程序,它获取一个商店库存,并搜索该库存中的特定项目,将这些项目推送到一个数组中。库存都是一个对象,此对象中的每个对象都是库存的一个项目。这些项本身没有键——它们只是对象文字。因此,我一直在使用快速枚举(针对产品中的项)遍历它们。每个项目如下所示:

{   id: 2759167427,   
    title: 'Practical Silk Bag',   
    handle: 'practical-silk-bag',  
    vendor: 'Legros, Willms and Von',  
    product_type: 'Bag'
}
我试图做的是将item对象推送到一个数组,当且仅当该项是键盘或计算机时。为此,我尝试采用以下方法:

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);
        }
    }
}
然而,我得到一个错误,告诉我includes不是一个已定义的方法,这意味着程序没有将item[title]识别为字符串,所以现在我被卡住了。我该如何避免这种情况?感谢您的帮助


欢呼所有

在循环的第一次迭代中,检查id是否包含字符串,但id是数字。因此,includes失败

我不确定您的意图是什么,但您可能只想检查.includes项是否为字符串

if(item[property]==“string”&(item[property].包括(“计算机”)| | item[property].包括(“键盘”){


如果您打开一些控制台日志,您可以看到发生了什么。

更新了

我将实现更改为循环对象而不是数组。我认为这就是您要寻找的

也许这是一个简单的一点,我相信它会为你工作

// 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));

希望这对你有用!

我可能遗漏了一些东西,但这些东西不只是字符串(不是对象)吗?不,这些东西是我第一次剪掉的代码中用花括号括起来的所有东西。因此它有属性“id”、“title”、“handle”等。现在我看到了,我的宝贝,我猜你在寻找一种“product\u type”的产品属性等于计算机或键盘,对吗?这是正确的。我已经尝试删除了“包含并只是纯粹的”if(item[property]=“Computer”| | item[property]=“Keyboard”)"…但是,当我的代码运行和编译时,当我使用console.log进行测试时,它不会打印任何内容。到目前为止,我一直在进行调试。我会尝试一下,让您知道结果!它编译和运行良好,但当我在'if'语句中插入console.log时,不会打印任何内容。程序只是运行到完成。您能将代码转换为jsfiddl吗e、 jsbin?我相信如果我们看到整个事情都在进行,我们可以很快解决它。太好了。我非常乐意帮忙