Determine the type of an Object in JavaScript

In javascript, all the variables are declared as var. However, sometimes we need to know the type of var to implement your logic. This is how you can determine some of the data types.

//chk if an object is an array or not.
function isArray(obj) {
//returns true is it is an array
    if (obj.constructor.toString().indexOf(“Array”) > -1){
        return true;
    }
    else{
        return false;
    }
}

To get the datatype, do the following:

var boolVal = true;
var numVal = 123;
var strVal = "String";
alert(typeof boolVal)    // displays "boolean"
alert(typeof numVal)     // displays "number"
alert(typeof strVal)     // displays "string"

Convert Dictionary to an Array of KeyValue Collection [C#]

Here, I will try to load a dictionary into an array of a custom class (having a key/value pair) and also into the array of KeyValuePair.
Let’s say you have a class structure as the following:-

public class FieldValueAsTextEntity
{
    private string key;
    public string Key
    {
        get { return key; }
        set { key = value; }
    }

    private string value;
    public string Value
    {
        get { return this.value; }
        set { this.value = value; }
    }
}

Now to convert a Dictionary to the array of the class, FieldValueAsTextEntity, do the following:-


FieldValueAsTextEntity[] arrayCollection = dictionaryObject.Select(pair=> new FieldValueAsTextEntity()
{
	Key = pair.Key,
	Value = pair.Value
}).ToArray();

OR,

if you sinply want to get the Dictionary object into the array of KeyValuePair, then, do the following:-


KeyValuePair<string, string>[] lstStringCollection;
lstStringCollection = dictionaryObject.Select(pair => new KeyValuePair<string, string>(pair.Key, pair.Value)).ToArray();

NOTE: Here, we have passed the key & Value to the constructor of the KeyValuePair, as its Key & Value property is readonly.More info