I am working on a class assignment where I need to make write a Loop. I made an array of objects for houses. I created a For Loop to sort the array by the price of the houses. The array displays in the console of the webpage but not in the actual webpage. Below are the extracts of what the console shows, what the webpage shows, and my code.
console log
(4) [{…}, {…}, {…}, {…}]
0: {id: 'house4', stories: 1, squareFoot: 1130, color: 'white', year: 1961, …}
1: {id: 'house1', stories: 1, squareFoot: 1285, color: 'beige', year: 2018, …}
2: {id: 'house2', stories: 1, squareFoot: 1560, color: 'brown', year: 1983, …}
3: {id: 'house3', stories: 2, squareFoot: 2975, color: 'tan', year: 2008, …}
length: 4
[[Prototype]]: Array(0)
webpage
[object Object],[object Object],[object Object],[object Object]
I have watched several videos about sorting arrays, but they all only display to the console log. I tried to modify my code to be similar to the example used in my lecture video as well, but still no output.
let houses = [
{id:"house1", stories:1, squareFoot:1285, color:"beige", year:2018, bedrooms:3, bathrooms:2, price:285000},
{id:"house2", stories:1, squareFoot:1560, color:"brown", year:1983, bedrooms:3, bathrooms:3, price:345000},
{id:"house3", stories:2, squareFoot:2975, color:"tan", year:2008, bedrooms:4, bathrooms:3, price:415000},
{id:"house4", stories:1, squareFoot:1130, color:"white", year:1961, bedrooms:2, bathrooms:2, price:215000}];
for (i=0; i<1; i++) {
houses.sort((a, b) => a.price - b.price);
document.getElementById("houses").innerHTML=houses;
}
console.log(houses);
<p id="houses"></p>