Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

i got a dynamic json object that can contain different type of attributes and objects inside, could have plane strings or even arrays. I made a javascript code to convert a single json structure to an HTML table, worked great but id like to make it for a dynamic json, so basically i would need to iterate through the json tree parents and childs to see how do i create this HTML table.

But i do have some problems when trying to validate if a child has an object inside, like this: ( i dont want to add to many details to the json)

parent: {
    child_1: {
        attr1 : value1
    },
    child_2: {
          [{ attribues and values in an array }]
    }
}

How could i achieve this? I was thinking of using the "typeof" function like so:

if (typeof key === 'array') {
    // do something
}else{
    // do another stuff
}

But i don't believe it would work well, can you guys help me?

Thanks in advance.

share|improve this question

marked as duplicate by Roman C, Minko Gechev, Frank Schmitt, rekire, tkanzakic May 9 '13 at 6:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I think it should work OK. Use a recursive function to build up the table. –  Barmar May 6 '13 at 19:40
    
@Barmar can you help me by giving me an example please? i was thinking about that too, since every 'array' i find, i will create a "linkable" row to add another pop up for all that data :( –  msqar May 6 '13 at 19:41
    
Try to write it yourself, then someone will help you fix it. I don't have an example handy. –  Barmar May 6 '13 at 19:43
    
typeof key will return "object" for arrays, thus typeof key === 'array' will always be false –  Vadim May 6 '13 at 19:43
    
why it will be false for array??? :S –  msqar May 6 '13 at 19:46

2 Answers 2

up vote 1 down vote accepted

Checking typeof key === 'array' is incorrect since for arrays typeof will return "object". You can try to use instanceof instead:

if (key instanceof Array) {
    // do something
} else {
    // do another stuff
}

But this will fail if your JSON was created in another frame. Another option is to check toString()

Object.prototype.toString.call(key).indexOf('Array') > 0

or to check

Array.isArray(key)

but it does not supported by all browsers.

Description of typeof you can see here https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof

share|improve this answer
    
See here for reasons why instanceof doesn't always work. –  Cᴏʀʏ May 6 '13 at 19:53
    
Bad choice to use indexOf("Array"), what if the object is a Typed Array, eg Int32Array –  Xotic750 May 6 '13 at 20:05
    
@Xotic750 OP mentioned that original object is JSON. According to JSON spec json.org it may contain only plain arrays (not typed) –  Vadim May 6 '13 at 20:09
    
I think you will find he is talking about a plain javascript object, like his example, and really not talking about JSON. stackoverflow.com/questions/2904131/… –  Xotic750 May 6 '13 at 20:11

Use something like this for testing if something is an array. This will then use Array.isArray if the browser supports it, or it will fall back to using Object.toString for identification.

Javascript

var isArray = (function () {
    if (typeof Array.isArray === "function") {
        return Array.isArray;
    }

    return function (array) {
        return Object.prototype.toString.call(array) === "[object Array]";
    };
}());

alert(isArray([]))

on jsfiddle

share|improve this answer
    
Care to comment on why the down vote? –  Xotic750 May 8 '13 at 13:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.