最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if variable is an object or an array? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Nov 10, 2018 at 3:49 Irvan HilmiIrvan Hilmi 4912 gold badges5 silver badges18 bronze badges 3
  • array instanceof Array //Output true , can also check is it array or not – Jameel Moideen Commented Nov 10, 2018 at 4:14
  • @NinjaJami how to implement it? array instanceof Array ? – Irvan Hilmi Commented Nov 10, 2018 at 4:15
  • 1 You can do like var arr=[1]; if(arr instanceof Array){} – Jameel Moideen Commented Nov 10, 2018 at 4:17
Add a comment  | 

4 Answers 4

Reset to default 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

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.

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]);
  }  
}

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.

发布评论

评论列表(0)

  1. 暂无评论