0

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"}

2 Answers 2

1
// using fs.readFileSync is fine, but only during process boot time
// if the data is dynamic you'll need to read it async
const data = (fs.readFileSync('./pretty.out'), 'utf8')
  .split('\n')
  .filter(line => line.trim())
  .map(line => JSON.parse(line));

// input should use post requests         
post(req, res) {
    const query = req.body;
    let results = data.filter(d => d.bar === query);
    res.json(results);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response! can these 2 functions (trim and filter) be together as a single function?
1

Looks like you've got it in array format now, so this should work fine:

// To return multiple query matches:
function search(query){
    return arr.filter(function(item){
        return JSON.stringify(item).match(query) !== null;
    })
}


// To return single (first) query match:
function search(query){
    return arr.find(function(item){
        return JSON.stringify(item).match(query) !== null;
    })
}

If you're not converting it to an array of json object you can use:

function search(query){
    return file.match(new RegExp("{.+"+query+".+}", "g"))
}

Here's a fiddle: https://jsfiddle.net/1cqacj3b/11/ (modified to show results in html

3 Comments

Thanks for the quick response! I guess this would work for a single find but i need to go through each line in the file and see if the string is present. If the string is found then the entire line should be returned.
Any of these should work for that. Are you converting the file to an array or leaving it as text? The regex one will work for the later. It will return all lines with a query match.
I added a js fiddle to illustrate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.