简体   繁体   English

用req.query填充数组

[英]Fill array with req.query

I want to fill an array with req.params, so i can edit my code easily right now the code is like this . 我想用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]

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++);

If you can use Object.values : 如果可以使用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: 或使用Object.keys如果您不能:

 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 : 如果要检索有限数量的参数,可以添加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); 

Actually your API should take an array instead, like: 实际上,您的API应该改为使用数组,例如:

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

And you can get the array like this: 你可以得到这样的数组:

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

More info 更多信息

Assuming rep.query properties reliably start with t and end with numbers: 假设rep.query属性可靠地以t开头并以数字结尾:

 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. Object.values并不完全可靠,因为您不能总是依靠已按顺序添加的属性。

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: 但是您有另一个示例,您的参数以't'开头,后跟数字:

 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: 最后,您还有一个限制,从t0到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 at, 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 : 或更通用一点,取所有t<number> params ,但在initend之间切片:

 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, eg if you skip t2, we go from t0 to t20, so once more: 这不检查的顺序是正确的,如果你跳过T2例如,我们从T0到T20去,所以一次:

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM