简体   繁体   English

将路径参数验证为对象数组

[英]Validate route params as array of object

Says user can go here example.com/?page=1&q=123 说用户可以转到此处example.com/?page=1&q=123

I can get [{page: 1}, {q: 123}] 我可以得到[{page: 1}, {q: 123}]

using a library 使用图书馆

But this is still can be dangerous says I want to use [{page: 1}] for something because the user can enter extra params which I don't need. 但是说我想对某些东西使用[{page: 1}]仍然很危险,因为用户可以输入我不需要的额外参数。

so the idea is I provide a preset const preset = [{page: 1}, {valid: true}] 所以我的想法是我提供一个预置const preset = [{page: 1}, {valid: true}]

how can I get [{page: 1}, {valid: true}] from says [{page: 1}, {valid: true}, {abc: 123}, {other: 'others'}] ? 我如何[{page: 1}, {valid: true}] from says [{page: 1}, {valid: true}, {abc: 123}, {other: 'others'}]

 var arr = [{page: 1}, {valid: true}, {abc: 123}, {other: 'others'}]; var result = arr.filter(x => Object.keys(x).includes('page') || Object.keys(x).includes('valid')); console.log(result); 

Use filter and Object.keys to iterate through the array and by checking key return obj 使用filterObject.keys遍历数组并检查返回键obj

 const mixArray = [{page: 1}, {valid: true}, {abc: 123}, {other: 'others'}]; function filterRequiredObj(arr=[],filters=[]){ return arr.filter((f)=>{ const keys= Object.keys(f)[0]; if(filters.includes(keys)) return f }) } const newArray = filterRequiredObj(mixArray,['page','valid']) console.log('newArray',newArray) 

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

相关问题 VueJS 路由参数对象 - VueJS route params object 未定义不是一个对象(评估'route.params.title') - Undefined is not an object (evaluating 'route.params.title') 如何通过将参数放在 object 中将参数传递给路由 - How to Pass params to a route by putting them in an object TypeError: route is undefined and undefined is not an object (评估'route.params') - TypeError: route is undefined and undefined is not an object (evaluating 'route.params') 验证数组 object - Swagger/NestJS - Validate array object - Swagger/NestJS 在非 object 评估 props.route.params.source 中反应原生 undefined - React native undefined in not an object evaluation props.route.params.source TypeError: undefined is not an object (评估'_route$params.useremail') - TypeError: undefined is not an object (evaluating '_route$params.useremail') TypeError: undefined 不是 object(评估 'route.params.id') - TypeError: undefined is not an object (evaluating 'route.params.id') TypeError: undefined is not an object(评估'route.params.myText') - TypeError: undefined is not an object (evaluating 'route.params.myText') VueJS - 过滤对象数组并将 $route.params 与属性值进行比较 - VueJS - Filtering array of objects and comparing the $route.params with the property value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM