0

I have checked some links to compare two array but

I want to compare and check if the value which are present in database are similar to value which are present in fileName

Problem is fileName is not and Array and I am passing Value to it via For Each from another function so it can be any value

fileName= JavaScript
fileName= Java
fileName= NodeJs
fileName= ReactJs
fileName= Oops

but singleProduct is array that's why I used for loop to extract name from it.

code:

function checkDoc(data, childProduct, fileName, pathName, req, res) {
  return new Promise((resolve, reject) => {
    Document.findAll({
      raw: true,
      where: {
        product_id: childProduct.id,
      },
    })
      .then((productDoc) => {
  
        productDoc.forEach((singleProduct) => {

          if (singleProduct.name === fileName) {
            //print matched value
          } else {
            //print unmatched value
          }
        });
      })
      .catch(function (err) {
        return reject(
          getFailureResponseJson("Can't be added please try again :) " + err)
        );
      });
  });
}

value present inside singleProduct.name

Oops
Java
JavaScript

value present inside fileName

Oops
Java
JavaScript
NodeJs
ReactJs

Need Output like this

Matched Value:
Oops
Java
JavaScript

Unmatched Value:
NodeJs
ReactJs
Aakash
  • 139
  • 1
  • 3
  • 22
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Apr 20 '21 at 13:34
  • You should name variables that hold arrays in plural (`filenames`, `productDocs`). You say there are multiple values present in `filename`, but you also say "*`fileName` is not and Array*" - I have not been able to figure whether it is an array of `filenames` or not from your description? Also "*but `singleProduct` is array*" seems wrong - `productDoc` is an array, `singleProduct` is not. – Bergi Apr 20 '21 at 13:38
  • Hi @Bergi I have update the question please have a look :) – Aakash Apr 20 '21 at 13:42
  • Wait, you're calling `checkDoc` multiple times? Are you passing multiple `childProduct` objects, do you want to make multiple database requests? – Bergi Apr 20 '21 at 13:46
  • yes @Bergi but by checking same data not present in my checkDoc – Aakash Apr 20 '21 at 13:47
  • If you do multiple database request, you get *different* `productDoc`s for each `filename`s, so it's really unclear what arrays you want to compare. – Bergi Apr 20 '21 at 13:51
  • I want to compare names which are present inside Database which I am getting by singelProduct.name and i want to compare this name with all the values which are coming inside fileName and those values who does not match print seperately – Aakash Apr 20 '21 at 13:54
  • But you can't compare those with "*all the values which are coming inside `fileName*" if you only pass a single filename to each call. You need to pass all of them, as an array – Bergi Apr 20 '21 at 14:07
  • ok @Bergi after passing them all in an array how i will compare them ? – Aakash Apr 20 '21 at 14:10
  • You got the `productDocs` array and the `filenames` array and then you [compute the difference](https://stackoverflow.com/q/1187518/1048572) as described in the links you found – Bergi Apr 20 '21 at 15:30

1 Answers1

0

You can try storing the values on to 2 arrays.

...
const matchedValues = [];
const unmatchedValues = [];

if (singleProduct.name === fileName) {
  matchedValues.push(fileName)
} else {
  unmatchedValues.push(fileName)
}
...

// Print 
grace
  • 11