1

I fetched data from database so its coming in string format and I want to check this string with my array data

  • my string values come like
fish.jpg
animal.jpg
fish.pdf
animal.pdf
mammal_bio.pdf
fish_bio.jpg
fruit_bio.pdf

I want to compare this data with my array which contain

["mammal_bio.pdf","fruit_bio.pdf","animal_bio.pdf","tree_bio.pdf"]

So i want to compare all the array values which contain _bio.pdf and store them

  • matchedArray=["mammal_bio.pdf","fruit_bio.pdf"
  • unmatchedArray=["animal_bio.pdf","tree_bio.pdf"]
2
  • I don't understand what's the question Commented May 31, 2021 at 15:10
  • 2
    What is your input and expected output? Commented May 31, 2021 at 15:13

4 Answers 4

1

Don't use filter but forEach or for loop instead because you don't need to loop through all items again to get the two arrays.

const input = `fish.jpg
animal.jpg
fish.pdf
animal.pdf
mammal_bio.pdf
animal_bio.pdf
fish_bio.jpg
tree_bio.pdf
fruit_bio.pdf`;

check = ["mammal_bio.pdf", "fruit_bio.pdf", "animal_bio.pdf", "tree_bio.pdf"];

const matched = [];
const unmatched = [];
input
  .split("\n")
  .forEach(item =>
    check.slice(0, 2).includes(item)
      ? matched.push(item)
      : check.slice(-2).includes(item)
      ? unmatched.push(item)
      : null
  );

console.log({ matched });
console.log({ unmatched });

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

3 Comments

I want only _bio.pdf in unmatched and matched matched= ["mammal_bio.pdf","fruit_bio.pdf"] and unmatched=["tree_bio.pdf",animal_bio.pdf"] not .jpg files @ikhvjs
Hello @ikhvjs i have modified my question can i get matched array and unmatched array differently ?
@Vikas, I updated. I guess you want to take the first two items as matched items and the last two items as unmatched items.
1

First you can filter out the strings which endsWith _bio.pdf. Then for matched result filter with fiterArr and similarly for unmatched result

let filterArr = ['mammal_bio.pdf','fruit_bio.pdf'];

let bioArr = arr.filter(a => a.endsWith('_bio.pdf'));

let matched =  bioArr.filter(b => filterArr.includes(b));

let unmatched =  bioArr.filter(b => !filterArr.includes(b));

Comments

0

Just use

Array.filter

method.

If you return true the item will be inside the new Array, false excludes the item.

const noBioList = []

const bioList = list.filter(item => {
    if (item.includes('_bio.pdf') {
        return true
    } else {
        noBioList.push(item)
        return false
    }
}

console.log(bioList)
console.log(noBioList)

Comments

0
const input = `fish.jpg
  animal.jpg
  fish.pdf
  animal.pdf
  mammal_bio.pdf
  animal_bio.pdf
  fish_bio.jpg
  tree_bio.pdf
  fruit_bio.pdf`;

const inputList = input.split(/\n\s*/g)

const check = ["mammal_bio.pdf", "fruit_bio.pdf"];

const { matched, unmatched } = inputList.reduce((result, file) => {
  if(/_bio\.pdf/.match(file)) {
    if(check.indexOf(file) < 0) result.unmatched.push(file);
    else result.matched.push(file)
  }
  return result
}, { 
  matched: [],
  unmatched: []
})

Comments

Your Answer

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