gpt4 book ai didi

javascript - 从多个数据源(json)搜索

转载 作者:行者123 更新时间:2023-12-03 00:30:57 24 4
gpt4 key购买 nike

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

下面是片段。用例:

  1. Search term: sample hybrid

  2. Expected behaviour: `It should compare each word within each data source and return the respective object in "searchResult['first']" & "searchResult['second']". i.e 'sample' should be searched across 2 data sources and return respective object and same should be for word 'hybrid'.

我做错了什么并得到一个空对象。

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/lodash@4.17.4/lodash.min.js"></script>

最佳答案

将单词转换为正则表达式(带有 i 标志 - 忽略大小写)并使用 RegExp.test() 查找该单词是否包含在 中>产品名称:

// 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/lodash@4.17.4/lodash.min.js"></script>

代码中的问题是立即返回的 compareEachWord() 函数。由于您希望函数仅在结果为 true 时立即返回,如果结果不是 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/lodash@4.17.4/lodash.min.js"></script>

关于javascript - 从多个数据源(json)搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53873372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com