2010-07-22 89 views
7

我希望能夠通過其中一個字符串,確定是否一個Javascript對象是一個「複雜」的對象或只是一個字符串

'this is a string' 

或JavaScript對象,

{one: 'this', two: 'is', three: 'a', four: 'string' } 

爲一個函數的參數,並根據它是一個字符串還是一個對象採取不同的操作。我如何確定哪一個是真的?

具體而言,我想遍歷一個對象的屬性,並做了一些分析,如果一個屬性是一個字符串,但窩遞歸如果屬性是一個對象。我已經想通了如何使用$.each()遍歷對象的屬性,但如果我只是做這個用字符串,它treates字符串作爲字母的排列,而不是一個單一的東西。我能以其他方式繞過這個嗎?

回答

6
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的LIB中的相似的方法,如$.isArray()$.isFunction()

本地JavaScript

for(var element in data){ 
    if(toString.call(element) === '[object Object]'){ 
     // we got an object here 
    } 
} 

要使用hack'ish方式與toString的優點,即可以識別是否是really對象和array。通過使用typeof element,對象和數組都將返回object

長話短說,你不能依靠typeof運營商來區分真正的objectsarrays。爲此,您需要toString.call()。如果你只需要知道它是否是任何物體,typeof就好。

+0

對於任何人想知道爲什麼我標記這個答案正確,而不是其中的一個更多的投票:在這個答案有一個關於如何滿足我的需求使用jQuery的建議,這意味着我不必寫(或詳細瞭解)任何實施使用它。這個答案也爲覆蓋函數和數組提供了很好的選擇。 – 2010-07-22 10:37:59

2

嘗試使用typeof運算符。對於字符串,它將返回object,並返回string

5
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 
} 

UPDATE:

一些進一步的考慮因素:

  1. 參見@Andy E'sÇ在下面省略。

  2. typeof null回報"object"爲好。這同樣適用於任何其他對象,包括數組。

+2

+1,因爲OP特意詢問字符串文字,但請記住'typeof new String(「Hello」)==「object」'。要絕對確定,請使用'a.constructor == String'。 – 2010-07-22 10:25:27

+0

你不能用typeof來區分'objects' /'arrays'和'null'。 – jAndy 2010-07-22 10:33:36

+0

@jAndy:是的。我在答覆中說過。 – 2010-07-22 10:38:38

4

試試這個:

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 
    } 
} 

感謝Andy Eargument.constructor的TIPP。

+3

什麼是反對票?我會很感激評論!所以我知道我做錯了什麼。 – jigfox 2010-07-22 10:23:19

+0

@Jens F.我同意。好答案。這裏投下的票是完全無用的。從我這裏取+1 :-) – 2010-07-22 10:30:54

+0

由於typeof(null)==='object'',所以調用'some_function(null)'將會失敗。 – jAndy 2010-07-22 10:31:42

1

你可以做這樣的事情

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

} 
+0

你應該去'else if' ... – 2010-07-22 10:27:02

+0

更新!感謝那 – AutomatedTester 2010-07-22 10:53:13

1

我有一個類似的問題,我想我找到了一個解決方案。這裏是我感興趣的任何人的示例代碼。

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" 

我從DavidPirek.com得到靈感 - 謝謝Pirek先生!

相關問題