javascript. converting result of an array to my browser screen not console.log

Benedict

Well-Known Forumite
const people = [
{name: "Don", age: 35, job: "dentist"},
{name: "Amy", age: 55, job: "doctor"},
{name: "Bruce", age: 26, job: "nurse"}
]
const overthirty = people.filter(person => {
return person.age >= 30;
});
document.write(overthirty),
This gives me ['object,Oject, 'object,Object']. how do I get the array content to screen not 'object?
This may look a bit naive but I am 77 and learning JS


document.write(overthirty);
 

Cue

Well-Known Forumite
JavaScript:
const people = [
{name: "Don", age: 35, job: "dentist"},
{name: "Amy", age: 55, job: "doctor"},
{name: "Bruce", age: 26, job: "nurse"}
]
const overthirty = people.filter(person => {
  // filter is a array method that creates a new array with all elements that pass the test implemented by the provided function
  return person.age >= 30;
});

for (let i = 0; i < overthirty.length; i++) {
  // for loop starts here, with i = 0 as the starting point
  // it will continue as long as i is less than the length of the overthirty array
  // it will increment i by 1 after each iteration
  const person = overthirty[i];
  // get the current person from the array using the index i
  const p = document.createElement("p");
  // create a new p element
  p.innerHTML = "Name: " + person.name + ", Age: " + person.age + ", Job: " + person.job;
  // set the innerHTML of the p element using string concatenation
  document.body.appendChild(p);
  // append the p element to the body of the page
}

I actually had ChatGPT write this for me, and add the comments, as it was easier than typing it out on my phone. Highly recommend having a play with it as you can have it explain how it works, comment it, etc.
 

Benedict

Well-Known Forumite
Remarking the explanation of each line of code is helpful as I can use the much of the code and in other instances. now have a better understanding of the process. I am eager to progress..
 
Top