作者:ANANREMEMBERO38_810 | 来源:互联网 | 2023-09-09 18:03
我正在使用.includes函数根据它们的字符串内容过滤一些记录。
下面是我的代码
var oav = ["baluardo"]
...
if (((item.metadata["pico:record"]["dc:description"]["_"]).length >= 10 &&
(item.metadata["pico:record"]["dc:description"]["_"]).length <= 400) && (item.metadata["pico:record"]["dc:description"]["_"])
.includes(oav)
)
... do something ...
使用一个术语.includes功能正常工作,再添加一个这样的术语
var oav = ["baluardo","Fortezza"]
不起作用,我有一个空数组。
建议?
致谢
您可以使用.every()
来检查oav
中的每个元素是否都在元数据中。
var oav = ["baluardo","Fortezza"]
//not actual metadata
var meta = ["a","b","c","d","e","f","g","h","i","j","baluardo","Fortezza"];
if (meta.length >= 10 && meta.length <= 400 && oav.every(o => meta.includes(o))) {
console.log("meta includes baluardo and Fortezza");
}
,
使用两个some()
函数将搜索词与description
值进行比较:
const result = searchTerms.some(word => inputArr.some(el => el.desc.includes(word)))
function findSearchTerms(inputArr,searchTerms){
if (inputArr.length >= 0 && inputArr.length <= 10 && searchTerms.some(word => inputArr.some(el => el.desc.includes(word)))){
console.log("one or more of these elements includes a search term")
}else{
console.log("none of these elements includes a search term")
}
}
const terms = ["baluardo","Fortezza"]
let arr = [{desc:"baluardo etc."},{desc:"etc. Fortezza"},{desc:"Other desc"}]
findSearchTerms(arr,terms)
arr = [{desc:"etc."},{desc:"etc."},terms)