3

If you use node.js and ejs and render JavaScript object to ejs, the resultant HTML page has the following syntax:

[object Object]

despite the fact that my object is as follows:

[{"a": 3, "b": 10}, {"c":3, "d":20}, {"e":1, "f":55}]

However, I want to render the object itself (object literal if I understand it correctly), not the useless [object Object].

So how can I render it properly? res.render("index", {result: listOfObject.valueOf()}) didn't work.

2 Answers 2

3

[object Object] is what you get when you call .toString() on an anonymous object. This is implicitly done when you concatenate with another string (e.g. "my object: " + {a:'b'}).

If you want to get the output you're looking for, you need to use

JSON.stringify(yourObjectHere)

Which prints it all out nicely.

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

Comments

3
res.render("index", { result: JSON.stringify(listOfObject) });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.