1

I want to fill an array with req.params, so i can edit my code easily right now the code is like this .

let test = [req.query.t0,req.query.t1,req.query.t2,req.query.t3,req.query.t4,req.query.t5,req.query.t6,req.query.t7,req.query.t8,req.query.t9,req.query.t10,req.query.t11]

Is there a way to fill easily my array with a loop or a fill map ?

something like this.

let init = 0;
let end = 19;
let test = Array(req.query.init-req.query.end+1)
.fill()
.map(() => init++);

4 Answers 4

4

If you can use Object.values:

var req = { query: { t0: 't0', t1: 't1', t2: 't2' }};
var params = Object.values(req.query);
console.log(params);

Or with Object.keys if you cannot:

var req = { query: { t0: 't0', t1: 't1', t2: 't2' }};
var params = Object.keys(req.query).map(key => req.query[key]);
console.log(params);

In case you want to retrieve a limited number of parameters, you can add array.prototype.filter:

var req = { query: { t0: 't0', t1: 't1', t2: 't2', t3: 't3', t4: 't4' }};
var nbParams = 3;
var params = Object.keys(req.query).filter((key, i) => i < nbParams).map(key => req.query[key]);
console.log(params);

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

3 Comments

Thank you sir working like a charm, is there a way to do a loop and to add i++ after every t ?
Something like this t[i], i++, i>20 is it possible ?
my request is like this http://localhost:5002/89EyATXtAyEb96T7z7?l=fr&n=HeyOh&[email protected]&[email protected]&m2=&m3=&t4=&m5=&m6=&m7=&m8=&m9=&m10=&m11=&m12=&m13=&m14=&m15=&m16=&m17=&[email protected]&m19=&v=4.9.6&hw=6.7 I want to take all the emails and not the other query
1

Assuming rep.query properties reliably start with t and end with numbers:

function fillArrayFromReq(query, start, end) {
  return Array.from({ length: end - start }, (_, i) => {
    const propNum = i + start;
    return query[`t${propNum}`];
  });
}

const query = {
  t0: 0,
  t1: 1,
  t2: 2,
  t3: 3,
  t4: 4,
  t5: 5,
  t6: 6,
  t7: 7,
  t8: 8,
  t9: 9,
}

console.log(fillArrayFromReq(query, 4, 7));

Object.values isn't entirely reliable because you can't always count on properties to have been added in order.

Comments

1

Actually your API should take an array instead, like:

yourapi?t=one&t=two&t=three

And you can get the array like this:

 req.query.t // ["one", "two", "three"]

More info

1 Comment

@taieb yup and thats wrong. you should change the query
0

Let me just aggregate and expand on the other nice answers. To get all the params values:

const query = { a: 'a val', num: 4, t0: 't0 val', t1: 't1 val' };
let array = Object.values(query); // -> [ val1, val2, val3 ...]
console.log(array);

// or with [{key: value}] pairs
array = Object.keys(query)
  .map(key => ({[key]: query[key]})); // -> [ { param: val1 }, { anotherParam: val2 }, ... ] 
console.log(array);

But you had a different example, your params were starting with 't' followed by number:

const query = { a: 'a val', num: 4, t0: 't0 val', t1: 't1 val' };
const array = Object.keys(query)
  .filter(key => /^t\d+$/.test(key)) // only take params whose keys start with a letter t, followed only by a number.
  .map(key => query[key]);
console.log(array);

Finally, you also had a limit, from t0 to t19:

const query = { a: 'a val', num: 4, t0: 't0 val', t1: 't1 val', t19: 't19 val', t20: 't20 val' };
const array = Object.keys(query)
  .filter(key => /^t([0-9]|[01][0-9])$/.test(key))    // match keys starting with a t, followed by a number between 0 and 19
  .map(key => query[key]); // or .map(key => ({[key]: query[key]}))
console.log(array);

Or a bit more generic, take all t<number> params, but slice between init and end:

const query = { a: 'a', num: 4, t0: 't0', t1: 't1' };
let init = 0;
let end = 19;
const array = Object.keys(query)
  .filter(key => /^t\d+$/.test(key))
  .map(key => query[key]) // or .map(key => ({[key]: query[key]}))
  .slice(init, end); 
console.log(array);

This doesn't check that the order is correct, e.g. if you skip t2, we go from t0 to t20, so once more:

const query = { a: 'a', num: 4, t0: 't0', t1: 't1' };
let init = 0;
let end = 19;
const array = Object.keys(query)
  .filter(key => /^t\d+$/.test(key))
  .filter(key => {
    let num = parseInt(key.substring(1), 10);
    return num >= init && num <= end;
  })
  .map(key => query[key]); // or .map(key => ({[key]: query[key]}));
console.log(array);

4 Comments

Sometimes it can be an empty, value on the t[i].
Should not be a problem, it'll just be empty string.
/^t\d+$/.test is not a function
What environment are you working on? Try running (or copy-pasting directly to your JavaScript environment), it should work. I've edited all my examples so they are running here.

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.