速记功能用于检查属性是否存在 [英] Shorthand function for checking whether a property exists

查看:136
本文介绍了速记功能用于检查属性是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们能帮我做一个简短的函数来确定一个对象属性是否存在吗?在99%的情况下,我想用它来检查返回的json对象是否包含指定的属性。请注意,不能保证任何父属性或甚至是json对象本身都必须被定义。



我正在用这种方式考虑一些事情:

 函数propertyExists(< property>){
//属性类似于data.property.property
返回typeof (data)!==undefined&& typeof(data.property)!==undefined&&& typeof(data.property.property)!==undefined;
}

我不知道如何以动态方式编写它来检查所有父属性。另外,in-parameter应该只是对data.property.property的引用而不是一个字符串,所以我不知道如何在其中找到父属性。

b
$ b

 函数isDefined(target,path){
if(typeof target!='object'|| target == null){
返回false;
}

var parts = path.split('。');

while(parts.length){
var branch = parts.shift();
if(!(目标分支)){
return false;
}

target = target [branch];
}

返回true;
}

它应该是这样使用的:

  var data = {foo:{bar:42}}; 
isDefined(data,foo); // true
isDefined(data,foo.bar); // true
isDefined(data,notfoo); // false
isDefined(data,foo.baz); // false

您可以轻松地调整它以返回值本身(或 null )而不是 true / false



更新:在阅读问题的评论之后,我搜索了运算符中的Javascript ,并将 typeof 用这个测试。现在代码被写成它的意图是怎样的。


Can you guys help me make a shorthand function determining whether an object property exists? In 99% of the cases I want to use it to check whether the returned json object contains the specified property or not. Note that there is no guarantee that any of the parent properties or even the json object itself must be defined.

I was thinking of something in this manner:

function propertyExists(<property>) {
    // property is something like data.property.property
    return typeof(data) !== "undefined" && typeof(data.property) !== "undefined" && typeof(data.property.property) !== "undefined";
}

I don't know how to write it in a dynamic manner to check all the parent properties. Also the in-parameter should be just a reference to the "data.property.property" and not a string, so I don't know how to find the parent properties within that either.

解决方案

Here's a function I have lying around from a project that tells you if a property is defined (including all of its parent properties, if any):

function isDefined(target, path) {
    if (typeof target != 'object' || target == null) {
        return false;
    }

    var parts = path.split('.');

    while(parts.length) {
        var branch = parts.shift();
        if (!(branch in target)) {
            return false;
        }

        target = target[branch];
    }

    return true;
}

It's supposed to be used like this:

var data = { foo: { bar: 42 } };
isDefined(data, "foo"); // true
isDefined(data, "foo.bar"); // true
isDefined(data, "notfoo"); // false
isDefined(data, "foo.baz"); // false

You can easily tweak this to return the value itself (or null) instead of true/false.

Update: After reading the comments on the question, I googled the Javascript in operator and replaced the typeof test with that. Now the code is written "how it was meant to be".

这篇关于速记功能用于检查属性是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆