【问题标题】:Accessing specific objects in an object of objects访问对象对象中的特定对象
【发布时间】:2016-06-13 07:27:15
【问题描述】:

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

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

当且仅当该项目是键盘或计算机时,我正在尝试将项目对象推送到数组。为此,我尝试使用这样的东西:

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

但是我收到一个错误,告诉我包含不是定义的方法,这意味着程序没有将 item[title] 识别为字符串,所以现在我被卡住了。我将如何规避这一点?任何帮助表示赞赏。

祝大家好运

【问题讨论】:

  • 我可能在那里遗漏了一些东西,但这些项目不只是字符串(不是对象)吗?
  • 否定,该项目是我第一个代码片段中用大括号括起来的所有内容。所以它具有“id”、“title”、“handle”等属性。
  • 现在我明白了,我的错
  • 我猜您正在寻找“product_type”属性等于计算机或键盘的产品,对吧?
  • 没错。我已经尝试删除“包含并且只使用纯粹的“if(item[property] == "Computer" | | item[property] == "Keyboard")"...但是,当我的代码运行和编译时,当我用 console.log 测试时它什么也不打印

标签: javascript string loops object


【解决方案1】:

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

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

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

如果您输入一些控制台日志,您可以看到发生了什么。 https://jsfiddle.net/qexssczd/1/

【讨论】:

  • 这就是我到目前为止的调试方式。我会试一试,让你知道发生了什么!
  • 它编译并运行良好,但是当我在'if'语句中粘贴一个console.log时,没有任何打印。程序刚刚运行完成
  • 你能把你的代码变成 jsfiddle、jsbin 或 SO MCVE 吗?我敢肯定,如果我们看到整件事情在行动,我们可以快速修复它。
【解决方案2】:

更新

我将实现更改为遍历对象而不是数组。我想这就是你要找的。​​p>

Here is a working jsBin

可能这有点简单,我相信它会为你工作

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

希望这对你有用!

【讨论】:

  • 太棒了。我很乐意提供帮助
猜你喜欢
  • 2017-06-12
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 2018-02-18
相关资源
最近更新 更多