0

I have variable data below

var data = [
{
    id: 1,
    name: "John",
    sex: "M",
    maritalStatus: "M",
    dob:"01-01-1990",
    title:"Software Engineer",
    address:"VN",
    phoneNumber:"(123) 456-7890",
    email: "john@gmail.com"
}
];

var data = [
    ["1", "John", "M", "M", "1990", "Software Engineer", "john@gmail.com", "(123) 456-7890"],
];

Is there anyway to know their type.I'm using typeof(data) but both show it's object.

  • 3
    Arrays are objects in Javascript. http://stackoverflow.com/questions/4775722/check-if-object-is-array – Roope Jul 09 '16 at 16:53
  • 1
    `isArray()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray – Stephen Thomas Jul 09 '16 at 16:54
  • `typeof` lies lots of times, but here it's correct. `data` belongs to Object type. – Oriol Jul 09 '16 at 16:58
  • There are a lot of hacks depending on what you are trying to achieve, e.g. `data.constructor.name`. But it's better to use "feature detection" like `isArray(data)` and act accordigly. – sbedulin Jul 09 '16 at 17:03

2 Answers2

0

Array.isArray(data) will return true since data is an array.

EDIT:

After clarification in the comments of this answer, Array.isArray(data[0]) will check if the first element in the array data is an array.

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • I have try that one but both data return true –  Jul 09 '16 at 17:01
  • Array.isArray(data) should return true since data is an array. – Charlie Fish Jul 09 '16 at 17:02
  • Both is an array.The first one is object object,Second on is object Array –  Jul 09 '16 at 17:08
  • Oh so you are wanting the first one to return false and the second to return true? – Charlie Fish Jul 09 '16 at 17:10
  • yes @Charlie Fish.How can I achived that –  Jul 09 '16 at 17:12
  • Ok sorry didn't quite understand your question. As long as it's the first element you are wanting to check "Array.isArray(data[0])" without the quotes should work. I will also edit my question now to reflect that. BUT it has to be the first element you are wanting to check. That will basically check to see if the first element inside of data is an array. – Charlie Fish Jul 09 '16 at 17:14
  • Thanks Charlie and also sorry for my bad language :D –  Jul 09 '16 at 17:18
  • @NgôHùngPhúc It's totally ok! Glad I could help in the end!! :) – Charlie Fish Jul 09 '16 at 17:18
0

you can use Object.prototype.toString().call(object)

var data = 
{
 id: 1,
 name: "John",
 sex: "M",
 maritalStatus: "M",
 dob:"01-01-1990",
 title:"Software Engineer",
 address:"VN",
 phoneNumber:"(123) 456-7890",
 email: "john@gmail.com"
};

console.log(Object.prototype.toString.call(data) ); //[object Object]


var data2 = [
{
 id: 1,
 name: "John",
 sex: "M",
 maritalStatus: "M",
 dob:"01-01-1990",
 title:"Software Engineer",
 address:"VN",
 phoneNumber:"(123) 456-7890",
 email: "john@gmail.com"
}
];


console.log(Object.prototype.toString.call(data2) ); //[object Array]

var data1 = [
               ["1", "John", "M", "M", "1990", "Software Engineer", "john@gmail.com", "(123) 456-7890"],
            ];

console.log(Object.prototype.toString.call(data1) );//[object Array]
Deep
  • 9,594
  • 2
  • 21
  • 33