我正在从多个数据源中搜索字符串

以下是代码段。用例:


  
  搜索词:sample hybrid
  预期的行为:`它应该比较每个数据源中的每个单词,并在“ searchResult ['first']”和“ searchResult ['second']”中返回相应的对象。
  即应在2个数据源中搜索“样本”,并返回各自的对象,“混合”一词应相同。
  


我做错了事,得到了一个空对象。



var dataSource1 = [{
  "_id": "5968dd23fc13ae04d9000001",
  "product_name": "sildenafil citrate",
  "supplier": "Wisozk Inc",
  "quantity": 261,
  "unit_cost": "$10.47"
}, {
  "_id": "5968dd23fc13ae04d9000002",
  "product_name": "Mountain Juniperus ashei",
  "supplier": "Keebler-Hilpert",
  "quantity": 292,
  "unit_cost": "$8.74"
}, {
  "_id": "5968dd23fc13ae04d9000003",

  "product_name": "Sample HBr",
  "supplier": "Schmitt-Weissnat",
  "quantity": 211,
  "unit_cost": "$20.53"
}];

var dataSource2 = [{
  "_id": "5968dd23fc13ae04d90000021",
  "product_name": "Hybrid product",
  "supplier": "Wisozk Inc",
  "quantity": 261,
  "unit_cost": "$10.47"
}, {
  "_id": "5968dd23fc13ae04d90000022",

  "product_name": "Mountain Sample Juniperus ashei",
  "supplier": "Keebler-Hilpert",
  "quantity": 292,
  "unit_cost": "$8.74"
}, {
  "_id": "5968dd23fc13ae04d90000033",

  "product_name": "Dextromathorphan HBr",
  "supplier": "Schmitt-Weissnat",
  "quantity": 211,
  "unit_cost": "$20.53"
}];

const searchCriteria = 'sample hybrid';
const searchResult = [];
function compareEachWord(str, searchCriteria) {
  var word = searchCriteria.split(" ");
  for(i=0; i<=word.length; i++) {
    return str.indexOf(word[i]) !== -1;
  }
}


function filterFromDS1() {
  const result =  _.filter(dataSource1, (item) => {
      return compareEachWord(_.toLower(item.product_name), searchCriteria);
  });
  return result;
}


function filterFromDS2() {
  const result =  _.filter(dataSource2, (item) => {
      return compareEachWord(_.toLower(item.product_name), searchCriteria);
  });
  return result;
}

searchResult['first'] = filterFromDS1();
searchResult['second'] = filterFromDS2();

console.log(searchResult);

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

最佳答案

将单词转换为RegExp(带有i标志-忽略大小写),然后使用RegExp.test()查找单词是否包含在product_name中:



// takes a criteria, converts to regex, and returns a filter function
const filterByCriteria = (searchCriteria) => {
  const searchRegex = new RegExp(_.words(searchCriteria).join('|'), 'i');

  // the filtering functions expects a data souce and filters it
  return (ds) => ds.filter(o => searchRegex.test(o.product_name));
};

/** Usage: **/

var dataSource1 = [{"_id":"5968dd23fc13ae04d9000001","product_name":"sildenafil citrate","supplier":"Wisozk Inc","quantity":261,"unit_cost":"$10.47"},{"_id":"5968dd23fc13ae04d9000002","product_name":"Mountain Juniperus ashei","supplier":"Keebler-Hilpert","quantity":292,"unit_cost":"$8.74"},{"_id":"5968dd23fc13ae04d9000003","product_name":"Sample HBr","supplier":"Schmitt-Weissnat","quantity":211,"unit_cost":"$20.53"}];

var dataSource2 = [{"_id":"5968dd23fc13ae04d90000021","product_name":"Hybrid product","supplier":"Wisozk Inc","quantity":261,"unit_cost":"$10.47"},{"_id":"5968dd23fc13ae04d90000022","product_name":"Mountain Sample Juniperus ashei","supplier":"Keebler-Hilpert","quantity":292,"unit_cost":"$8.74"},{"_id":"5968dd23fc13ae04d90000033","product_name":"Dextromathorphan HBr","supplier":"Schmitt-Weissnat","quantity":211,"unit_cost":"$20.53"}];

const searchCriteria = 'sample hybrid';

const filterDs = filterByCriteria(searchCriteria);

const searchResult = {
  first: filterDs(dataSource1),
  second: filterDs(dataSource2),
};

console.log(searchResult);

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>





您代码中的问题是compareEachWord()函数立即返回。由于您希望函数仅在结果为true时立即返回,而如果结果为Array.some()则循环到末尾,因此可以使用:



var dataSource1 = [{"_id":"5968dd23fc13ae04d9000001","product_name":"sildenafil citrate","supplier":"Wisozk Inc","quantity":261,"unit_cost":"$10.47"},{"_id":"5968dd23fc13ae04d9000002","product_name":"Mountain Juniperus ashei","supplier":"Keebler-Hilpert","quantity":292,"unit_cost":"$8.74"},{"_id":"5968dd23fc13ae04d9000003","product_name":"Sample HBr","supplier":"Schmitt-Weissnat","quantity":211,"unit_cost":"$20.53"}];

var dataSource2 = [{"_id":"5968dd23fc13ae04d90000021","product_name":"Hybrid product","supplier":"Wisozk Inc","quantity":261,"unit_cost":"$10.47"},{"_id":"5968dd23fc13ae04d90000022","product_name":"Mountain Sample Juniperus ashei","supplier":"Keebler-Hilpert","quantity":292,"unit_cost":"$8.74"},{"_id":"5968dd23fc13ae04d90000033","product_name":"Dextromathorphan HBr","supplier":"Schmitt-Weissnat","quantity":211,"unit_cost":"$20.53"}];

const searchCriteria = 'sample hybrid';
const searchResult = {};
function compareEachWord(str, searchCriteria) {
  var word = searchCriteria.split(" ");

  return word.some(w => str.includes(w));
}


function filterFromDS1() {
  const result =  _.filter(dataSource1, (item) => {
      return compareEachWord(_.toLower(item.product_name), searchCriteria);
  });
  return result;
}


function filterFromDS2() {
  const result =  _.filter(dataSource2, (item) => {
      return compareEachWord(_.toLower(item.product_name), searchCriteria);
  });
  return result;
}

searchResult['first'] = filterFromDS1();
searchResult['second'] = filterFromDS2();

console.log(searchResult);

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

09-27 01:03