javascript - 如何循环数组并检查

标签 javascript bixby

我需要循环一个如下所示的数据结构:

{tags: ["stonehenge"], text: "At about 40m high, Silbury Hill is the highest prehistoric mound in Europe and about the size of one of the smaller pyramids of the Giza Necropolis in Egypt. Construction started about 2400BC and is estimated (by R J C Atkinson) to have taken 500 men working full-time about 15 years to complete. It is hard to see how a single tribe could have found this man-power; rather it seems necessary to envisage a much larger social structure. The monument is contemporary with Stonehenge and part of the Stonehenge World Heritage Site, close to Avebury. Its function is not known.", speech: "Silbury Hill, built starting in 2400 BC, is the highest prehistoric mound in Europe and about the size of one of the smaller pyramids of the Giza Necropolis in Egypt.", title: ["Silbury Hill"], themes: ["photo", "Avebury", "UNESCO WHS"], image: {url: "images/Silbury_Hill.jpg"}}, ...

并仅提取那些在主题对象中找到值 searchTheme(例如“照片”)的项目。

我旨在完成此任务的函数是:

  console.log ("ready to search for searchTheme", searchTheme,"in", items.length,  "items")

  for (var j = 0; j < items.length; j++) {
    for (var k = 0; k < items[j].themes.length; k++) 
    console.log("examining item", j, "theme", k, "which is", items[j].themes[k],"for match with", searchTheme)
    if (searchTheme == items[j].themes[k] {
      console.log ("that was a match")
      matches.push(items[k])}

    }

它返回的东西看起来像是正确地循环数组,但由于某种原因,它不是——返回零命中。

enter image description here

enter image description here

最佳答案

我回到 Github 上的 bixby 示例存储库,并在事实胶囊中找到了一个模型。这有效:

   for (var i = 0; i < items.length; i++) {
    if (items[i].themes) {
      for (var j = 0; j < items[i].themes.length; j++) {
        if (searchTheme == items[i].themes[j].toLowerCase()) {
          matches.push(items[i])
          break
        }
      }
    }
  }
  return matches

不同的是第二行测试主题是否包含在对象中。老实说,我认为我在我的版本中做得对!但这是有效的。

关于javascript - 如何循环数组并检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59129720/

相关文章:

bixby - 如何删除在我的输入 View 中看到的 "None"对话驱动程序?

bixby - 无法使用文档中的代码访问 Bixby 的唯一标识符

validation - 如何修复验证提示中的通用对话框

javascript - r.js 由于 ENOENT 错误而无法构建...说找不到 ".bin/bower"

javascript - React-draggable 不适用于自定义组件

javascript - 不能在html5中嵌套链接?

reminders - 如何以编程方式设置 Bixby 提醒?

bixby - 我可以覆盖图书馆胶囊的NoResult对话框吗?

javascript - 无法通过 Angular $resource 发布数据?

javascript - 单击超时显示隐藏的 HTML 元素