admin管理员组文章数量:1336304
in nodejs I want to read some data into an array which has been saved previously to file. The original data is structured in an array. Example:
let arr = [
'id-001': [ '123', '246', '234' ],
'id-002': [ '789', '235' ],
... and so on
];
It is saved like this:
fs.writeFileSync(dirpath + 'data.txt', arr);
This part works fine. The file contains this string:
[
'id-001': [ '123', '246', '234' ],
'id-002': [ '789', '235' ],
... and so on
]
Now I thought I could just read the file
let data = fs.readFileSync(filePath, 'utf8');
...that works...
and do something like
eval('let arr = ' + data);
or
let arr = eval(data);
or even
const vm = require('vm')
let arr = vm.runInNewContext(arr, {data})
and some more. Nothing works. It behaves strange, just nothing seems to happen, code after the eval seems not to be executed. No errors.
What may be wrong? Is there a better way?
in nodejs I want to read some data into an array which has been saved previously to file. The original data is structured in an array. Example:
let arr = [
'id-001': [ '123', '246', '234' ],
'id-002': [ '789', '235' ],
... and so on
];
It is saved like this:
fs.writeFileSync(dirpath + 'data.txt', arr);
This part works fine. The file contains this string:
[
'id-001': [ '123', '246', '234' ],
'id-002': [ '789', '235' ],
... and so on
]
Now I thought I could just read the file
let data = fs.readFileSync(filePath, 'utf8');
...that works...
and do something like
eval('let arr = ' + data);
or
let arr = eval(data);
or even
const vm = require('vm')
let arr = vm.runInNewContext(arr, {data})
and some more. Nothing works. It behaves strange, just nothing seems to happen, code after the eval seems not to be executed. No errors.
What may be wrong? Is there a better way?
Share Improve this question asked Jul 5, 2019 at 13:35 R0bynR0byn 4231 gold badge6 silver badges18 bronze badges 8-
The
let arr = …
code at the beginning cannot work because it’s not valid JavaScript syntax. – Sebastian Simon Commented Jul 5, 2019 at 13:38 -
@sebastianSimon I guess its rather that
arr.toString()
is not valid JS ... – Jonas Wilms Commented Jul 5, 2019 at 13:45 - @JonasWilms It would be valid, if the syntax errors would be corrected. – Sebastian Simon Commented Jul 5, 2019 at 13:47
- I must be missing something ... Which syntax error? – Jonas Wilms Commented Jul 5, 2019 at 13:48
- 1 Oh right, then the OP is also wrong about the files contents ... The whole question then doesnt make any sense ... – Jonas Wilms Commented Jul 5, 2019 at 13:53
3 Answers
Reset to default 5You can parse the file content using JSON.parse
after reading it, that should make it work.
Also, you'll want to persist a JSON.stringify
ed representation of your data.
Here's a minimum example that shows the whole process:
const fs = require('fs');
function write(array, path) {
fs.writeFileSync(path, JSON.stringify(array));
}
function read(path) {
const fileContent = fs.readFileSync(path);
const array = JSON.parse(fileContent);
return array;
}
write(['a', 'b'], '/my/path/test.txt');
const arr = read('/my/path/test.txt');
console.log(arr);
When you write the file you should stringify
it:
fs.writeFileSync(dirpath + 'data.txt', JSON.stringify(arr));
Then you simply need to parse
the string data from the file back to a javascript object
let data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
Correct your array 1st and write/read the file
const fs = require('fs');
let dir = './data.txt';
let arr = {'id-001': [ '123', '246', '234' ],
'id-002': [ '789', '444' ]};
fs.writeFileSync(dir, JSON.stringify(arr));
let data = fs.readFileSync(dir, 'utf8');
console.log(data);
版权声明:本文标题:How to save an array to file and later read it into an array variable in nodejsJavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742390758a2465939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论