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

4
  • 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? Commented Dec 29, 2019 at 16:05
  • 4
    Make your life easier with an intermediate variable: const meta = item.metadata["pico:record"]["dc:description"]["_"] Commented Dec 29, 2019 at 16:05
  • Does this answer your question? multiple conditions for JavaScript .includes() method Commented Dec 29, 2019 at 16:06
  • @AndrewL64 all of those strings Commented Dec 29, 2019 at 16:08

2 Answers 2

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");
}

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.