0

无法让这个搜索引擎工作。搜索键很简单,数据很小,但我没有得到任何结果。有人可以指出我在哪里犯错吗?

这是代码:

var storedList = {
  "success": true,
  "message": "1519 records retrieved.",
  "count": 1519,
  "data": {
    "0": {
      "eNum": "000052",
      "qtr": "20Q3",
      "device": "Freight - Drum",
    },
    "1": {
      "eNum": "003957",
      "qtr": "20Q2",
      "device": "Passenger - Traction",
    },
    "2": {
      "eNum": "171235",
      "qtr": "19Q2",
      "device": "Passenger - Traction",
    },
    "3": {
      "eNum": "171056",
      "qtr": "19Q4",
      "device": "Vertical Enclosed",
    },
  }
}

function showSearchResult(searchTerm, key) {
  console.log('searchTerm: '+searchTerm)
  let options = {
    threshold: 0.6,
    keys: [key],
  }
  let fuse = new Fuse(storedList.data, options)
  let itemsOnly = {}
  let results = fuse.search(searchTerm)
  console.log('results: ', JSON.parse(JSON.stringify(results, null, 2)))
  for(var k in results) {
    itemsOnly[k] = results[k].item
  }
  storedList.data = itemsOnly
  storedList.message = 'search has been executed'
  storedList.count = Object.keys(itemsOnly).length
}
let result = showSearchResult('00','eNum')
console.log('storedList: ', JSON.parse(JSON.stringify(storedList, null, 2)))
for(var k in storedList.data) {
  console.log('item: '+k)
}

这是一个 JSbin 链接,以防你是其中之一:link

4

0 回答 0