1

I'm filtering some records according to the their string content using the .includes function.

Here a snap of my code


 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 ...

Using one term the .includes function works properly, adding one more term like this


var oav = ["baluardo", "Fortezza"]

doesn't work and I'm having an empty array.

Suggestions?

Regards

Pelide
  • 468
  • 1
  • 4
  • 19
  • 3
    So you want to check if your metadata includes atleast one of those strings in oav or you want to check if your metadata includes all of those strings in oav? – AndrewL64 Dec 29 '19 at 16:05
  • 4
    Make your life easier with an intermediate variable: `const meta = item.metadata["pico:record"]["dc:description"]["_"]` – jarmod Dec 29 '19 at 16:05
  • Does this answer your question? [multiple conditions for JavaScript .includes() method](https://stackoverflow.com/questions/37896484/multiple-conditions-for-javascript-includes-method) – Gerard Dec 29 '19 at 16:06
  • @AndrewL64 all of those strings – Pelide Dec 29 '19 at 16:08

2 Answers2

3

You can use .every() to check if every element in oav is in the metadata.

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");
}
Yousername
  • 1,012
  • 5
  • 15
0

Use two some() functions to compare the search terms to the description value:

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."},{desc:"Other desc"}]

findSearchTerms(arr,terms)
symlink
  • 11,984
  • 7
  • 29
  • 50