I am new to javascript and struggling to come up with a solution. I have got a file that contains lines in JSON format. In the below code I have converted the JSON to objects and trimmed the white space on each line and returned the output.
Now, I need to not only remove the white spaces but to even search for a string(provided by the user and passed in as a variable) in each line of the file and if the string found it should return the entire line.
I tried .includes(req.params.msg) but couldn't get to right.
get(req, res) {
let arry = [];
const text = (fs.readFileSync('./pretty.out'));
arry = (text.toString().split('\n'));
let wat = [];
arry.forEach(i => {
if (!!i.trim()) {
wat.push(JSON.parse(i));
}
});
res.json(wat);
}
File's content will be,
{"foo" : "bar","bar" : "sit"}
{"foo" : "lorem","bar" : "ipsum"}
{"foo" : "dolor","bar" : "amet"}
If the user inputs sit then the output should be,
{"foo" : "bar","bar" : "sit"}