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/[email protected]&[email protected]&m3=&m4=&m5=&m6=&m7=&m8=&[email protected]&m10=&[email protected]&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) // [email protected] , [email protected] , [email protected] , [email protected]

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 
8
  • Do you have any code showing what you've tried? Commented Mar 14, 2018 at 16:16
  • Will edit my respose @JacobHeater Commented Mar 14, 2018 at 16:18
  • I would encourage you to look at this question: stackoverflow.com/questions/9064127/… Commented Mar 14, 2018 at 16:19
  • @JacobHeater i think it's not the same subject Commented Mar 14, 2018 at 16:22
  • Given the details now in the question, I agree, they're not the same. Commented Mar 14, 2018 at 16:24

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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=&[email protected]

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

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

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

    console.log(params); // [ 'Has value', '', '', '[email protected]' ]

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

    console.log(withValues); // [ 'Has value', '[email protected]' ]

    res.sendStatus(200);

});

app.listen(80);

Comments

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.