-3
var people = [  
  {
    name: "Mike",
    age: 12,
    gender: "male"
  },{
    name: "Madeline",
    age: 80,
    gender: "female"
  }
]

How do i Loop through the array and log to the console "old enough" if they are 18 or older, and "not old enough" if thy aren't 18. how do i do it, any help, thanks in advance

NTP
  • 4,338
  • 3
  • 16
  • 24
Brukk
  • 5
  • 2
  • 2
    check the forEach array prototype method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – Jay Lane Mar 22 '18 at 18:48

7 Answers7

1
  for(const person of people)
    console.log(`is ${person.age < 18 ? "not" : ""} old enough`);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • It's a duplicate question (with typos as well) that has already been answered in the comments... but this is a valid and correct (and short) answer as well. – Jochem Schulenklopper Mar 22 '18 at 21:40
  • 1
    @jochem yup i admit i was to lazy to search for a dupe – Jonas Wilms Mar 22 '18 at 22:14
  • Doesn't matter. Your answer was flagged for review (because it is short) and I approved it because it is concise and correct (even though the original question was a dupe). – Jochem Schulenklopper Mar 24 '18 at 15:03
  • 1
    @jochem thanks :) i usually try to write code that speaks for itself and animate the OP to look into the docs, some people however dont seem to get that – Jonas Wilms Mar 24 '18 at 15:29
0
//Iterate your objects, check their age property to see if greater than equal to 18
for(let x = 0; x < people.length; x++) {
     if(people[x].age >= 18){
        console.log(people[x].name + " Is Old Enough");
     }else {
        console.log(people[x].name + " Is Not Old Enough");
     } 
}
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
0
people.forEach((node)=>{
    if(node.age > 18) {
    console.log("old enough")
  }else {
    console.log("not old enough")
  }
})
Murad Sofiyev
  • 790
  • 1
  • 8
  • 25
0

You can use the forEach Method.

 people.forEach((element)=>{
     element.age >= 18 ? console.log('Old Enough') : console.log('Not old enough')
 });
Jay Lane
  • 1,379
  • 15
  • 28
0

var people = [{
  name: "Mike",
  age: 12,
  gender: "male"
}, {
  name: "Madeline",
  age: 80,
  gender: "female"
}]

for (var i = 0; i < people.count; i++) {
  if (people[i].age < 18) {
    console.log("too young message");
  } else {
    console.log("old enough message");
  }
}
NappingRabbit
  • 1,888
  • 1
  • 13
  • 18
0

var people = [
{ name: "Mike", age: 12, gender: "male" },{ name: "Madeline", age: 80, gender: "female" } ];

people.forEach(person=>{
  console.log(person.age >= 18 ? "Old enough": "Not old enough");
  return;
});
George
  • 2,330
  • 3
  • 15
  • 36
0
people.map(
  item => {
     console.log(
       (item.age>18)?item.name+' is old enough':item.name+' is not old enough'
     )
   }
 )
  • 1
    I would use forEach instead of map here. Map returns a new array, while forEach simply iterates over each element. – Erty Seidohl Mar 22 '18 at 19:31