简体   繁体   English

如何从 Javascript 中的文本文件输入文件输入创建 Object?

[英]How to create Object from text file input file input in Javascript?

Hello there I am Stuck in a problem and I can't find a way out of it?你好,我陷入了一个问题,我找不到解决办法?

Below is the problem statement下面是问题陈述

Read the input file 'input.txt'.读取输入文件“input.txt”。

Create new object using new Object().使用 new Object() 创建新的 object。

Inside the object create a method named as checkLeapYear().在 object 内部创建一个名为 checkLeapYear() 的方法。

Using this method check the given year is a leap year or not.使用此方法检查给定年份是否为闰年。

Read the input from 'input.txt'.从“input.txt”读取输入。 input array with initial values.具有初始值的输入数组。 {S1: “John”, S2:”James”, S3:”Jack” } {S1:“约翰”,S2:“詹姆斯”,S3:“杰克”}

The code which is pre written is预先编写的代码是
var fs = require('fs');

var input = fs.readFileSync('input.txt').toString();

I tried it in another way我用另一种方式尝试过

var fs = require('fs');
var value = fs.readFileSync("input.txt");
console.log(typeof value);

It returns an object but if I try to access the values its showing nothing.它返回一个 object 但如果我尝试访问这些值,它什么也没有显示。

please help me with this code please check the image请帮我处理这段代码请检查图片

The object that is returned to you is a buffer that eventually converts to string form by converting to utf8, but you have no choice but to change your input data, so: Hi, you have two ways to do this:返回给您的 object 是一个缓冲区,最终通过转换为 utf8 转换为字符串形式,但是您别无选择,只能更改输入数据,所以:您好,您有两种方法可以做到这一点:

  1. Convert the "input.txt" file to "input.json" and save the json data in the file and after receiving it through the JSON.parse method, convert it to an object将“input.txt”文件转换为“input.json”并将json数据保存在文件中,通过JSON.parse方法接收后,转换为object
  2. Use RegExp使用正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM