0

I want to create a table and put on it all the param which are not empty in param like this

http://localhost:5002/5u3k0HRYqYEyToBB53m1=Test2@test.fr&m2=test3@test.com&m3=&m4=&m5=&m6=&m7=&m8=&m9=test6@test.com&m10=&m11=test10@test.com&m12=&m13=&m14=&m15=&m16=&m17=&m18=&m19=&v=4.1.2&hw=6

And after this i want to store all the email ( m1,m2,m9,m11 ) in a table.

console.log(b) // test2@test.fr , test3@test.com , test10@test.com , test6@test.com

So I did it like this

    let emailTable = [req.query.m0,req.query.m1,req.query.m2......]
Box.push({Email:emailTable}}
console.log(Box)// it show me even the empty param, i want only the full one 
Taieb
  • 920
  • 3
  • 16
  • 37

2 Answers2

0

Here is simple solution to the problem JSFIDLE: https://jsfiddle.net/85bzj3a3/7/

// Lets imagine this object is your req.param
reqParam = {
  m1: 'email1',
  m2: '',
  m3: 'email3',
  m4: 'email4',
  someOther1: 'some other param',
  m5: '',
  m61: 'email6',
  someOther: 'otherValue'
}

const  emailTable = []
for (let key of Object.keys(reqParam)){
    // check if it is email and if it has value
  if(key.match(/^[m]+[0-9]*$/g) != null && reqParam[key] != ''){
    emailTable.push(reqParam[key])
  } 
}
console.log(emailTable)

What I did, is go through paramerers, check if it is email parameter using regex, and then check if it has value, if it does, push it to emailTable.

I dont think it is smart to hard code all parameters when pushing to array, since next time you add another parameter in url, you have to hardcode it again in your function, which is no bueno.

noitse
  • 1,045
  • 9
  • 27
0

Here's my solution to your problem. I am using lodash to make things a bit easier to read. The native implementation to this problem should be pretty easy to figure out. My index.js file contains VERY simple Express server that can print query string parameters to the console. Let me know if you still have questions.

// Request URL: http://localhost/?&m1=Has%20value&m2=&m3=&m4=test@mail.com

const express = require('express');
const app = express();
const _ = require('lodash');

app.get('/', (req, res) => {

    const params = _.values(req.query);

    console.log(params); // [ 'Has value', '', '', 'test@mail.com' ]

    const withValues = _.filter(params, p => !!p);

    console.log(withValues); // [ 'Has value', 'test@mail.com' ]

    res.sendStatus(200);

});

app.listen(80);
idream1nC0de
  • 1,171
  • 5
  • 13