使用req.query

时间:2018-03-21 07:59:23

标签: javascript node.js

我想用req.params填充一个数组,所以我现在可以轻松编辑我的代码,代码是这样的。

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]

有没有办法用循环或填充图轻松填充我的数组?

像这样的东西。

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

4 个答案:

答案 0 :(得分:4)

如果您可以使用.appear { background-color: rgba(29, 29, 29, 0.9); position: absolute; display: none; padding: 0 10px 10px 10px; z-index: 1000000; color: white; font-size: 13px; } .hover { cursor: pointer; color: #666; display: inline-block; } .hover:hover .appear { display: block; }



<p>
  There is no exact definition on when a short story is too long, thus a
  <span class="hover">
    novella
    <span class="appear">A novella is a text that is longer than a short story, but not quite long enough to be a novel.</span>
  </span>
  or a
  <span class="hover">
    novel
    <span class="appear">A novel is relatively long piece of fiction. What we often refer to as "a book".</span>
  </span>
  on the other hand too short, becoming merely an
  <span class="hover">
    anecdote
    <span class="appear">an anecdote is a short tale</span>
  </span>
  , to fit into the genre, but most short stories that we work with in the lessons are less than ten pages.
</p>
&#13;
&#13;
&#13;

如果不能,请使用Object.values

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

如果您想要检索有限数量的参数,可以添加Object.keys

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

答案 1 :(得分:1)

实际上,您的API应该采用数组,例如:

keytool

你可以得到这样的数组:

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

More info

答案 2 :(得分:1)

假设rep.query属性可靠地从t开始并以数字结尾:

&#13;
&#13;
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));
&#13;
&#13;
&#13;

Object.values并非完全可靠,因为您无法始终指望按顺序添加属性。

答案 3 :(得分:0)

让我汇总并扩展其他好的答案。要获得所有参数值:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

但是你有一个不同的例子,你的参数开始于&#39; t&#39;其次是数字:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

最后,您还有一个限制,从t0到t19:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

或者更通用一点,取所有t<number> params,但在initend之间进行切片:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

这不会检查订单是否正确,例如如果你跳过t2,我们会从t0到t20,所以再一次:

&#13;
&#13;
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);
&#13;
&#13;
&#13;