JS:对一个函数使用不同的输入数据,并检查是否没有变量为空

JS: Use different input data for a function and check if no variable is empty

本文关键字:检查 数据 输入 是否 变量 一个 函数 JS      更新时间:2023-09-26

我想我撞错了方向,现在我真的被卡住了。这就是为什么我请求你的帮助

摘要:我试图在函数的两个可能的输入数据之间切换,我需要检查空值,以获得有效的结果数组。数组结构是固定的(我无法更改),如果缺少任何值或输入对象不匹配(例如元素少于预期),结果应该是data = false

有一个函数只有一个参数(template)。但输入不同:

首先

  1. 我必须检查数据是否作为一个简单的对象进入函数

template = { type: 'anything', 0: 'Book title', 1: 'Article title', 2: 'Name', 3: '1' }

  1. 如果有一个ReactiveDict-变量(我会这样访问它:template.dict.get('type')),数据必须从那里获取

在后一种情况下,数据也存储在变量template中,但我必须稍微更改对它的访问:template.dict.get('type')template.type

  1. 也可能是根本没有(正确的)数据的情况:例如,没有数据,或者一个对象的元素少于或多于预期

所以我尝试使用这个,在两种输入类型之间切换:

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

检查对象是否存在。