1

I was studying up on the .filter() method in Javascript. I came across this example online.

var heroes = [
        {name: "Batman", franchise: "DC"},
        {name: "Ironman", franchise: "Marvel"},
        {name: "Thor", franchise: "Marvel"},
        {name: "Superman", franchise: "DC"}
    ];

  var marvelHeroes = heroes.filter(function(hero) {
      return hero.franchise == "Marvel";
  })

  document.write(marvelHeroes);

I expect to get an array of objects showing only the Marvel heroes. However, when I try to print the results of the marvelHeroes variable, I am getting the following result:

[object Object],[object Object]

Can someone tell me what is wrong here?

3
  • 2
    You cannot output an array to the document. Either use console.log(marvelHeroes) or document.body.innerText = JSON.stringify(marvelHeroes); Closing this. Commented Jan 21, 2019 at 19:55
  • 1
    Use document.write(JSON.stringify(marvelHeroes)); and check the output. Commented Jan 21, 2019 at 19:55
  • 3
    Possible duplicate of How to document.write javascript object Commented Jan 21, 2019 at 19:57

3 Answers 3

2

The issue is, you're sending it raw JavaScript objects. As others have pointed out, if you stringify this array, the error disappears. The reason that [object Object] shows up is that the browser can only put strings in HTML. So in order to coerce those objects into strings, it uses the Object#toString method, which results in each method being turned into a string as [object Object]. By using JSON.stringify to serialize the objects before writing it to the document, you ensure that the objects are properly represented in string form.

const object = { a: 'a', b: '2', c: 3 };
const stringOne = object.toString();
const stringTwo = JSON.stringify(object);

document.write(stringOne);
document.write(stringTwo);

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

Comments

2

You need to convert the JS data structure to a string to view it on the page. For that you use stringify. Further, to get a nice formatted output, and add the string to a pre element with a nice indentation.

var heroes = [{"name":"Batman","franchise":"DC"},{"name":"Ironman","franchise":"Marvel"},{"name":"Thor","franchise":"Marvel"},{"name":"Superman","franchise":"DC"}];

var marvelHeroes = heroes.filter(function(hero) {
  return hero.franchise == "Marvel";
})

const pre = document.querySelector('pre');
pre.textContent = JSON.stringify(marvelHeroes, null, 2);
<pre></pre>

Comments

1

You try to write object, before that you need to stringify it (to json)

document.write(JSON.stringify(marvelHeroes));

var heroes = [
        {name: "Batman", franchise: "DC"},
        {name: "Ironman", franchise: "Marvel"},
        {name: "Thor", franchise: "Marvel"},
        {name: "Superman", franchise: "DC"}
    ];

  var marvelHeroes = heroes.filter(function(hero) {
      return hero.franchise == "Marvel";
  })

  document.write(JSON.stringify(marvelHeroes));

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.