Output:
My code:
let carsArray = [
{
make: "BMW",
model: "325i",
year: 2034,
},
{
make: "Volvo",
model: "540xi",
year: 2025,
},
{
make: "BMW",
model: "435e",
year: 1994,
},
{
make: "BMW",
model: "X6",
year: 2025,
},
];
function twoYearsOld (element) {
let year = element.year - 2023;
return year === 2;
};
let twoYearOldCars = carsArray.filter(twoYearsOld);
document.write(twoYearOldCars);
Hey Everyone. I have an issue where when I write document.write
, the outcome on the live page just says object, object. I have an array with objects in it.
I am expecting it to say this:
[
{
make: 'Volvo',
model: '540xi',
year: 2025
},
{
make: 'BMW',
model: 'X6',
year: 2025
}
]
2
Answers
I think, you are using a wrong method to display your result. you can use console.log() or JSON.stringify() like this
With Console.log
console.log(twoYearOldCars);
With JSON.stringify()
document.body.innerText = JSON.stringify(twoYearOldCars);
Now let see how the code looks
It sounds like you want to "prettify" the data. You can stringify it, and then add it to a
<code>
element inside a<pre>
element. ref1 | ref2.