6

I have a problem that i didn't know how to solve it, i have test some information of how i should comparing or checking a variable if it was an array or an object

I have tried this

console.log({} == []); // return false
console.log({1:"haha"} == {}); // return false
console.log(["haha"] == {}); // retun false

The problem is, that i want to know if a variable is actualy an object cause typeof of both [] or {} return object.

console.log(isobject({1:"haha"})) // should return true;
console.log(isobject(["haha"])); // should return false;

Or

console.log(isobject({})) // should return true;
console.log(isobject([])); // should return false;

Is there any function to check variable like above? Thanks for any correction.

3
  • array instanceof Array //Output true , can also check is it array or not Commented Nov 10, 2018 at 4:14
  • @NinjaJami how to implement it? array instanceof Array ? Commented Nov 10, 2018 at 4:15
  • 1
    You can do like var arr=[1]; if(arr instanceof Array){} Commented Nov 10, 2018 at 4:17

4 Answers 4

10

This would help.

var a = [], b = {};

console.log(Object.prototype.toString.call(a).indexOf("Array")>-1);
console.log(Object.prototype.toString.call(b).indexOf("Object")>-1);

console.log(a.constructor.name == "Array");
console.log(b.constructor.name == "Object");

There are many other ways, but the above is backward compatible in all browsers.

Related questions must be referred:

Check if a value is array

Check if a value is object

Sign up to request clarification or add additional context in comments.

4 Comments

Why the indexOf check, and not just a plain comparison? You can also check the name of the constructor
how can i check the name of the constructor? @CertainPerformance
@IrvanHilmi Updated the answer.
@IrvanHilmi eg [].constructor.name === 'Array' (though Array.isArray is better for arrays) and ({}).constructor.name === 'Object'
5

arr = [1,2,3]

Array.isArray(arr) // should output true

for object I would do obj = {a:1}

Object.keys(obj).length // should output 1

so you could do

Object.keys(obj).length >= 0 // should be true if obj is obj literal.

1 Comment

Object.keys(obj).length // should output 1 arr = [5] will also output 1 for Object.keys(arr).length
1

var jsonData = {
  Name:"Jonh Doe",
  Dept: {
    code: "cse",
    title: "Computer science & tech"
  },
  Courses: [
    {
      code: "cse123",
      name: "something"
    },{
      code: "cse123",
      name: "something"
    }
  ]
}

for(var item in jsonData){
  if(typeof jsonData[item] === "object"){
    var x = "";
    if(Array.isArray(jsonData[item])) x = "Array";
    else x = "object";
    console.log(item + " is " + x);
  }else{
    console.log(item + " is " + typeof jsonData[item]);
  }  
}

Comments

0
JSON.stringify(variable).startsWith("[")

This will return true when variable is an array.

JSON.stringify(variable).startsWith("{")

This will return true when variable is an object.

1 Comment

This is extremely inefficient. The fact that this is even suggested is outrageous.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.