JS:对一个函数使用不同的输入数据,并检查是否没有变量为空
JS: Use different input data for a function and check if no variable is empty
我想我撞错了方向,现在我真的被卡住了。这就是为什么我请求你的帮助
摘要:我试图在函数的两个可能的输入数据之间切换,我需要检查空值,以获得有效的结果数组。数组结构是固定的(我无法更改),如果缺少任何值或输入对象不匹配(例如元素少于预期),结果应该是data = false
有一个函数只有一个参数(template
)。但输入不同:
首先
- 我必须检查数据是否作为一个简单的对象进入函数
像
template = { type: 'anything', 0: 'Book title', 1: 'Article title', 2: 'Name', 3: '1' }
或
- 如果有一个
ReactiveDict
-变量(我会这样访问它:template.dict.get('type')
),数据必须从那里获取
在后一种情况下,数据也存储在变量template
中,但我必须稍微更改对它的访问:template.dict.get('type')
与template.type
- 也可能是根本没有(正确的)数据的情况:例如,没有数据,或者一个对象的元素少于或多于预期
所以我尝试使用这个,在两种输入类型之间切换:
const type = template.type || template.dict.get('type')
这不起作用,因为有时我会得到错误Cannot read property 'get' of undefined
。我认为如果根本没有输入类型,那么template.type
是未定义的,而没有ReactiveDict
,这意味着template.dict
是未定义。
第二个我需要检查是否所有变量都有值:每个(修剪的)值都需要是一个字符串。
为了将来的开发,我想验证每个变量的数据(我认为使用regEx),以使这个函数更好。示例:edition应该是一个数字(但存储为字符串)。
功能
function(template) {
const type = template.type || template.dict.get('type'), // How to do this better?
docId = '12345';
let data = false;
if (type === 'anything') {
const title = template[0] || template.dic.get(0).value, // How to do those better?
article = template[1] || template.dic.get(1).value,
author = template[2] || template.dic.get(2).value,
edition = template[3] || template.dic.get(3).value;
if (title && article && author && edition) // How to check each value smarter?
data = [
[
{ title: title.trim() },
{ article: article.trim() },
{ author: author.trim() },
{ reference: docId }
],
{
title: title.trim(),
type : type
},
{
article: article.trim()
},
{
author : author.trim(),
edition : edition.trim()
},
{
reference: docId
}
];
}
return data;
}
早上好,更换
const type = template.type || template.dict.get('type')
通过
const type = template.type || (template.dict && template.dict.get('type'))
我看到你使用es6,所以你可以做破坏性的做作:const[title,author,article,author]=模板但在它不能解决你必须检查它们是否未定义的事实之后,你可以使用对象代替
o = {}
var oneValueIsEmpty = false
["title","article","author","edtiton"].forEach(function(key,i){
o[key] = template[i] || template.dic.get(i).value
oneValueIsEmpty = oneValueIsEmpty || !o[key]
})
if (!oneValueIsEmpty )
data = [
[
{ title: o.title.trim() },
{ article: o.article.trim() },
{ author: o.author.trim() },
{ reference: docId }
],
{
title: o.title.trim(),
type : type
},
{
article: o.article.trim()
},
{
author : o.author.trim(),
edition : o.edition.trim()
},
{
reference: docId
}
];
您可以
const type = template.type || template.dict.get('type')
转换为
const type = template && template.dict
? template.dict.get('type')
: template.type;
和
const title = template && template.dict
? template.dict.get(0).value
: template[0];
编辑:此外,我建议在每个变量之前显式地写const和let,不要使用逗号声明多个变量。对于下一个必须维护代码的人来说,这总是一团糟,尽管这是非常固执己见的。
您正在声明:
const type = template.type || template.dict.get('type'),
但这里你指的是template.dc.get,而不是dict,这并没有多大帮助。
if (type === 'anything') {
const title = template[0] || template.dic.get(0).value, // How to do those better?
article = template[1] || template.dic.get(1).value,
author = template[2] || template.dic.get(2).value,
edition = template[3] || template.dic.get(3).value;
你可以使用:
if (typeof template.dict !== 'undefined')
检查对象是否存在。
- 如何检查数据表是否有任何包含字符串的行并获取它的行索引
- 有没有一种方法可以在Typescript中自动检查数据类型的数据相等性
- 修改脚本以检查数据相关
- AngularJS选择检查数据并插入
- 角度 - 检查数据是否存在
- Javascript:如何检查数据是否已加载
- 量角器:检查数据按日期排序
- 如何在节点JS中检查数据类型 - 特别是整数
- 检查数据属性是否具有值
- 如何使用 angularjs 在下拉列表更改后检查数据保存
- 如何检查数据列表中选中了哪个单选按钮
- 在mongo中检查数据并创建数据的有效方法'不存在
- 用于检查数据格式的Javascript无法正常工作
- 通过 jquery 对 json 格式的数据使用真/假条件检查数据是否存在
- 如何检查数据表行的可见性
- 在AngularFire中检查数据唯一性
- 检查数据类型jquery或javascript
- 如何检查数据是否退出json格式
- 检查数据集是否被更改(添加/编辑,删除记录)的最佳解决方案是什么?
- 我如何检查数据表是否包含我想要的值,并可能单击链接