Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在React/JS中格式化此文件的最佳方式是什么?_Javascript_Reactjs_Syntax - Fatal编程技术网

Javascript 在React/JS中格式化此文件的最佳方式是什么?

Javascript 在React/JS中格式化此文件的最佳方式是什么?,javascript,reactjs,syntax,Javascript,Reactjs,Syntax,我有一个组件,它使用多个道具来使用一个URL,该URL反过来调用JSON数据的API。道具由一系列选择框收集,选择框将选择作为道具发送,然后组件将这些道具安装到axios.get调用中,如下所示: axios.get(`/inventory_reporter_api/logs?stage=PRODUCTION&severity=${nextProps.selectedSeverity}&start=${nextProps.startDate}&end=${nextProp

我有一个组件,它使用多个道具来使用一个URL,该URL反过来调用JSON数据的API。道具由一系列选择框收集,选择框将选择作为道具发送,然后组件将这些道具安装到axios.get调用中,如下所示:

axios.get(`/inventory_reporter_api/logs?stage=PRODUCTION&severity=${nextProps.selectedSeverity}&start=${nextProps.startDate}&end=${nextProps.endDate}`)
这就是我的问题。如果缺少任何道具,那么axios API调用将无法工作。例如,在上面的示例中,如果我没有nextrops.selectedSeverity,api将返回一个错误。因此,我的问题是,上面的调用的语法是什么?如果有nextrops.selectedSeverity,则将“&severity=${nextrops.selectedSeverity}”添加到URL,如果没有,则不添加它。请提前感谢任何帮助。

尝试以下操作:

//用于演示目的
设nextrops={
起始日期:2019年1月1日,
截止日期:2019年1月10日
}
让url='/inventory\u reporter\u api/logs?'
+ [
{key:'severity',var:nextrops.selectedSeverity},
{key:'start',var:nextrops.startDate},
{key:'end',var:nextrops.endDate}
]
.map(o=>o.var?`${o.key}=${o.var}`:'')
.join(“&”)
console.log(url);

如果你有一个道具列表,这是相当简单的:

常量名称=[
“严重性”,
“优先权”,
“重要性”,
“紧迫性”
];
//…使用它。。。
常量nextrops={
选择的严重性:7,
选择重要性:“高”
};
让url=名称
.filter(name=>nextrops[“选定”+name]!==未定义)
.map(name=>name.toLowerCase()+“=”+encodeURIComponent(nextrops[“selected”+name]))
.加入(“&”);

log(url);
我将构建一个小实用程序来构建params对象

const buildParams = (input, paramKey, paramValue) => typeof paramValue !== 'undefined' ? { ...input, [paramKey]: paramValue } : input;

let params = { stage: 'PRODUCTION' };
params = buildParams(params, 'severity', nextProps.selectedSeverity);
params = buildParams(params, 'start', nextProps.startDate);
params = buildParams(params, 'end', nextProps.endDate);

axios.get('/inventory_reporter_api/logs', params)

只需检查这些值是否存在,并构建url字符串?谢谢你,尼诺,我会试试这个!问题是,如果下一步为空,它会在url中输入一个“&”,并导致错误。非常感谢你,T.J.!@CHays412-不用担心!如果这个答案或其他任何答案回答了你的问题,堆栈溢出的工作方式,你会“接受”“通过单击其旁边的复选标记,可以找到答案。”。但前提是你的问题真的得到了回答。