【问题标题】:How to store unmatched string from an object using JavaScript / Node js如何使用 JavaScript / Node js 存储对象中不匹配的字符串
【发布时间】:2021-07-16 07:43:59
【问题描述】:

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

  • 我有一个对象,我想检查字符串是否存在

  • 因为我有一个代码来检查对象中是否存在字符串

  • 如果字符串与值不匹配怎么办

  • 作为这个不匹配的值,我想保存在与fileName不匹配的数据库中

  • 我通过运行下面的循环代码得到了我的字符串fileName

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循环运行后就可以了

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

我想检查fileName 的值是否存在于我的对象(productDoc)中

  • 假设我的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)
}

【问题讨论】:

  • fileName 是什么?字符串、数字、数组、对象?
  • 它是一个字符串@nur
  • 举个例子会很有帮助。你想怎么匹配?​​
  • 好的,我会更新我的问题
  • 你好 Nur 我已经更新了问题看看:)

标签: javascript node.js nodes


【解决方案1】:

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

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

【讨论】:

  • 你好@nur 我的fileName 不是一个数组,因为我正在运行循环我在fileName 中得到的所有值都是字符串类型
  • 是的,创建了文件名数组。我可以直接将数组保存在数据库中吗?我不知道这个
  • 您不必创建fileName 的数组。我只是在for/of循环中使用它进行迭代...
【解决方案2】:

一种方法是从一个目录中读取所有文件

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

【讨论】:

  • 嗨@Angular我需要匹配的值来更新我的数据库和不匹配的值添加到数据库中
  • @Aakash,我已经更新了答案。我还没有测试过代码,但是应该是这样的。
猜你喜欢
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多