What does the [object Object] mean? (Javascript)

One of my warnings gives the following result:

[object Object] 

What does it mean? (This was a signal for some jQuery object.)

+30
javascript jquery
Jan 17 2018-12-12T00:
source share
6 answers

This means that you are warning an instance of an object. When alert input of an object, toString() is called on the object, and the default implementation returns [object Object] .

 var objA = {}; var objB = new Object; var objC = {}; objC.toString = function () { return "objC" }; alert(objA); // [object Object] alert(objB); // [object Object] alert(objC); // objC 

If you want to inspect an object, you must either console.log it, or JSON.stringify() , or list its properties and view them individually using for in .

+38
Jan 17 '12 at 9:50
source share

The alert () function cannot display an object in a readable form. Try using console.log (object) instead and start your browser console for debugging.

+5
Jan 17 2018-12-12T00:
source share

I wrote this answer in another question that was duplicated, and soon I want the answer to be close, so I will answer here. my two cents, and hope some other help anyway.

Since @Matt answered the reason [object object] , so you have three options for JSON.stringify(JSONobject) , console.log(JSONobject) or console.log(JSONobject) over the object, see the following basic example.

 var jsonObj={ property1 : "one", property2 : "two", property3 : "three", property4 : "fourth", }; var strBuilder = []; for(key in jsonObj){ if (jsonObj.hasOwnProperty(key)) { strBuilder.push("Key is " + key + ", value is " + jsonObj[key] + "\n"); } } alert(strBuilder.join("")); 

https://jsfiddle.net/b1u6hfns/

+3
Jun 03 '15 at 23:14
source share

If you embed it in the DOM, try wrapping it in

 <pre> <code>{JSON.stringify(REPLACE_WITH_OBJECT, null, 4)}</code> </pre> 

visual analysis is a little easier.

+1
Apr 03 '18 at 18:02
source share

Alerts are not the best for displaying objects. Try console.log? If you still see the Object in the console, use JSON.parse, like this> var obj = JSON.parse(yourObject); console.log(obj) var obj = JSON.parse(yourObject); console.log(obj)

0
Jan 6 '18 at 17:39
source share

Another option is to use JSON.stringify(obj)

For example:

 exampleObj = {'a':1,'b':2,'c':3}; alert(JSON.stringify(exampleObj)) 

https://www.w3schools.com/js/js_json_stringify.asp

0
Aug 06 '19 at 21:53
source share



All Articles