如何使用 JavaScript / Node js 从对象中存储不匹配的字符串
How to store unmatched string from an object using JavaScript / Node js
有点疑惑,不知道解决办法
我有一个对象,我想检查字符串是否存在
因为我有一个代码来检查对象代码中是否存在字符串
如果字符串与值不匹配怎么办
作为这个不匹配的值,我想保存在与 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 Loop之后可以是
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)
}
一种方法是使用
从一个目录中读取所有文件
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 });
有点疑惑,不知道解决办法
我有一个对象,我想检查字符串是否存在
因为我有一个代码来检查对象代码中是否存在字符串
如果字符串与值不匹配怎么办
作为这个不匹配的值,我想保存在与
不匹配的数据库中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 Loop之后可以是
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)
}
一种方法是使用
从一个目录中读取所有文件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 });