在nodejs的if语句中读取txt文件

时间:2022-12-03 19:25:08

I'm saving a some variable as array of object in txt file , the question is how can i read them and compare it with another variable in for loop .

我将一个变量保存为txt文件中的对象数组,问题是如何读取它们并与for循环中的另一个变量进行比较。

Thanks .

谢谢。

The format that i Saved in txt file :

我保存在txt文件中的格式:

[{"sekke":"445,675","halfsekke":"145,600"}]  

And the code that i want to read is :
NodeJs:

我想读的代码是:NodeJs:

var array = fs.readFileSync('data.txt').toString().split("\n");
            console.log(array[0]['sekke'])
            for (var key in array[0]['sekke']) {
                console.log("key " + key + " has value " + array);
            }  

which is wrong , how can i read achive something like this ?

这是错的,我怎么能读懂这样的东西?

if (array['sekke] == 100){
console.log("is ok");
}

2 个解决方案

#1


2  

Try with the below code:

试试下面的代码:

fs.readFile('data.txt', 'utf8', function read(err, data) {
       if (err) {
         throw err;
       }
       data = JSON.parse(data);
       var dataObject = data[0];
       for (i=0;i<Object.keys(dataObject).length;i++) {
           var ss = dataObject[i];
           var key = Object.keys(ss);
          for(varj=0;j<ss[key];j++){
             //your if condition logic
          }
       }  
    });

#2


2  

You need to parse the string back to JSON if you want to use it like an object. If your file is a collection of arrays (1 array per line) then split it by \n like you do and then parse it with JSON.parse. You would need to run that on each line separately thought so use a map or a loop. If your file however is one big array of objects then just read it in and then parse the whole thing with JSON.parse.

如果想要将字符串用作对象,则需要将字符串解析回JSON。如果你的文件是数组的集合(每行1个数组),然后像你一样将它拆分为\n,然后用JSON.parse解析它。您将需要在每一行分别运行它,因此使用映射或循环。如果你的文件是一个大数组的对象,那么就把它读进去,然后用JSON.parse来解析整件事情。

Also, consider using the asynchronous version of readFile as using readFileSync will block the whole node.js app while it's reading (that becomes a problem as the file gets bigger)

另外,考虑使用readFile的异步版本,因为使用readFileSync将阻塞整个节点。正在读取的js应用程序(当文件变大时就会出现问题)

#1


2  

Try with the below code:

试试下面的代码:

fs.readFile('data.txt', 'utf8', function read(err, data) {
       if (err) {
         throw err;
       }
       data = JSON.parse(data);
       var dataObject = data[0];
       for (i=0;i<Object.keys(dataObject).length;i++) {
           var ss = dataObject[i];
           var key = Object.keys(ss);
          for(varj=0;j<ss[key];j++){
             //your if condition logic
          }
       }  
    });

#2


2  

You need to parse the string back to JSON if you want to use it like an object. If your file is a collection of arrays (1 array per line) then split it by \n like you do and then parse it with JSON.parse. You would need to run that on each line separately thought so use a map or a loop. If your file however is one big array of objects then just read it in and then parse the whole thing with JSON.parse.

如果想要将字符串用作对象,则需要将字符串解析回JSON。如果你的文件是数组的集合(每行1个数组),然后像你一样将它拆分为\n,然后用JSON.parse解析它。您将需要在每一行分别运行它,因此使用映射或循环。如果你的文件是一个大数组的对象,那么就把它读进去,然后用JSON.parse来解析整件事情。

Also, consider using the asynchronous version of readFile as using readFileSync will block the whole node.js app while it's reading (that becomes a problem as the file gets bigger)

另外,考虑使用readFile的异步版本,因为使用readFileSync将阻塞整个节点。正在读取的js应用程序(当文件变大时就会出现问题)