如何使用 JavaScript / Node js 从对象中存储不匹配的字符串

How to store unmatched string from an object using JavaScript / Node js

有点疑惑,不知道解决办法

for (let i = 0; i <= readFileArray.length - 1; i++) {
      for (let j = 0; j < readFileArray[i].length; j++) {
        if (readFileArray[i][j] === comparePathName) {
          let fileName = readFileArray[1][j];
        }
      }
    }

所以这个文件名在运行 for Loop之后可以是

fileName: Java
fileName: Node JS
fileName: JavaScript
fileName: Asp.net
fileName: Oops

我想检查 fileName 的值不在我的对象 (productDoc)

productDoc = [
 {
    id: 1,
    name:Java,
    description: language
  },
  {
    id: 2,
    name:JavaScript,
    description: language
  },
  {
    id: 3,
    name:Oops,
    description: Subject
  }
]

所以我想通过 fileName

检查 productDoc 中出现的每个名字

匹配的值应该以不同的方式存储,不匹配的值应该以不同的方式存储 因为我需要将这个不匹配的值存储在我的数据库中匹配的值已经在数据库中

matched: Java
matched: JavaScript
matched: Node Js
unMatched: Asp.net
unMatched: Oops

所以我可以在 If else 条件下使用这个值

if(matched){
updateDocument(matched)
}else{
addDocument(unmatched)
}

一种方法是使用

从一个目录中读取所有文件
var fileList = [];
path = 'YOUR_PATH_TO_DIRECTORY';

fs.readdirSync(path).forEach(file => {
  
   fileList.push(file);    
   //this will read all the files and save push it in fileList
  
})

然后,您可以将读取的文件名与数组对象进行比较

var matchingValue = [];
var unMatchingValue = [];

for(var i = 0; i < this.productDoc.length; i++)
    {
        for(var j = 0; j < this.fileList.length; j++)
        {
          if(this.productDoc[i].name == this.fileList[j])
          {
            this.matchingValue[i].push(this.productDoc[i].name);

            //this will push the matching element from the productDoc
            //in the new array matchingValue
          }
          else if(this.product[i].name != this.fileList[j])
          {
            this.unMatchingValue[i].push(this.product[i].name);
            
          }
        }
    }

正如我在评论中所说,创建一个数组,然后检查它是否包含...

var fileName = ["Oops", "Java", "JavaScript", "NodeJs", "ReactJs"],
    productDoc = [{ name: "Java", }, { name: "JavaScript", }, { name: "Oops", }],
    productNames = Array.from(productDoc, item => item.name),
    matched = [],
    unMatched = [];

for (const name of fileName)
    productNames.includes(name) ? matched.push(name) : unMatched.push(name);

console.log({ matched, unMatched });