Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.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/1/cocoa/3.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对象是否为;复杂的;对象或只是一个字符串_Javascript_Jquery_String_Object - Fatal编程技术网

确定Javascript对象是否为;复杂的;对象或只是一个字符串

确定Javascript对象是否为;复杂的;对象或只是一个字符串,javascript,jquery,string,object,Javascript,Jquery,String,Object,我希望能够传递字符串文字 'this is a string' 或者一个javascript对象 {one: 'this', two: 'is', three: 'a', four: 'string' } 作为函数的参数,并根据它是字符串还是对象采取不同的操作。我如何确定哪个是正确的 具体来说,我希望迭代对象的属性,如果属性是字符串,则进行一些解析,如果属性是对象,则进行递归嵌套。我已经知道了如何使用$.each()迭代对象的属性,但是如果我只对字符串执行此操作,它会将字符串作为字母数组处理

我希望能够传递字符串文字

'this is a string'
或者一个javascript对象

{one: 'this', two: 'is', three: 'a', four: 'string' }
作为函数的参数,并根据它是字符串还是对象采取不同的操作。我如何确定哪个是正确的

具体来说,我希望迭代对象的属性,如果属性是字符串,则进行一些解析,如果属性是对象,则进行递归嵌套。我已经知道了如何使用
$.each()
迭代对象的属性,但是如果我只对字符串执行此操作,它会将字符串作为字母数组处理,而不是作为单个对象。我可以用其他方法解决这个问题吗?

试试接线员。它将为对象返回
object
,为字符串返回
string

var a = 'this is a string';

console.log(typeof a);   // Displays: "string"

var b = {one: 'this', two: 'is', three: 'a', four: 'string' };

console.log(typeof b);   // Displays: "object"
因此:

if (typeof yourArgument === 'string') {
   // Do the string parsing
}
else if (typeof yourArgument === 'object') {
   // Do the property enumeration
}
else {
   // Throw exception
}

更新:

一些进一步的考虑:

  • 见下面的评论

  • typeof null
    也返回
    “对象”
    。这同样适用于任何其他对象,包括数组

  • 试试这个:

    function some_function(argument) {
      if (typeof(argument) == "string" || argument.constructor == String) {
        // it's a string literal
      } else if (argument && typeof(argument) == "object" && argument.constructor != Array) {
        // it's an object and not null
      } else {
        // error
      }
    }
    
    感谢使用
    参数的tipp.constructor

    var data = {
        foo: "I'm a string literal",
        bar:  {
           content: "I'm within an object"
        }        
    };
    
    jQuery

    $.each(data, function(i, element){
        if($.isPlainObject(element){
           // we got an object here
        }
    });
    
    jQuery库中有类似的方法,如
    $.isArray()
    $.isFunction()

    本机Javascript

    for(var element in data){
       if(toString.call(element) === '[object Object]'){
          // we got an object here
       }
    }
    
    hack'ish
    方法与
    toString
    结合使用有一个优点,即您可以识别它是否真的是
    对象和
    数组。通过使用
    typeof元素
    ,对象和数组都将返回
    object


    长话短说,您不能依靠
    typeof
    操作符来区分真正的
    对象
    数组
    。为此,您需要
    toString.call()
    。如果您只需要知道它是否是任何对象,
    typeof
    就可以了。

    您可以这样做

    function something(variableX){
      if (typeof(variableX) === 'object'){
         // Do something
      }else if (typeof(variableX) === 'string'){
         // Do something
      }
    
    }
    

    我遇到了类似的问题,我想我找到了解决办法。这是我的示例代码,供感兴趣的人使用

    var propToDotSyntax = function (obj) {
        var parse = function (o, n) {
            var a = [], t;
            for (var p in o) {
                if (o.hasOwnProperty(p)) {
                    t = o[p];
                    if (n !== undefined) tmp = n + '.' + p;
                    else tmp = p;
                    if (t && typeof(t) === 'object') a.push(arguments.callee(t, tmp));
                    else a.push(tmp + '=' + t);
                }
            }
            return a;
        };
        return parse(obj).toString();
    }
    var i = { prop: 'string', obj: { subprop: 'substring', subobj: { subsubprop: 'subsubstring' } } };
    
    propToDotSyntax(i);
    
    这将遍历对象的所有属性(即使属性本身是对象),并以点语法返回包含以下值的字符串

    "prop=string,obj.subprop=substring,obj.subobj.subsubprop=subsubstring"
    

    我从中得到了灵感-谢谢皮雷克先生

    投了什么反对票?我将非常感谢您的评论!所以我知道我做错了什么。@Jens F。我同意。回答得好。在这里投反对票是完全没有用的。从我这里得到+1:-)这将无法调用
    some_函数(null)
    ,因为
    typeof(null)==“object”
    @jAndy。你迟到了一点,我已经更新了我的代码。@Jens F:确实,但仍然
    typeof
    不知道它是数组还是真实对象。+1,因为OP特别询问字符串文字,但请记住
    typeof new string(“Hello”)==“object”
    。为了绝对确定,请使用
    a.constructor==String
    。您无法区分带有typeof的
    对象
    /
    数组
    null
    。@jAndy:是true。我在回答中提到了这一点。对于任何想知道为什么我将这个答案标记为正确答案而不是更多投票的人:在这个答案中,有一个关于如何使用jQuery满足我的需求的建议,这意味着我不必编写(或详细理解)任何实现来使用它。这个答案也为覆盖函数和数组提供了很好的选择。