简体   繁体   中英

I need to loop through an array and find the people that are under age 18 . Should i convert it to String (toString) first?

I need to find the people that are under age 18 in the array using loops. Do i need to make it a string first?

I tried with creating a function, that loops through it, but I am not sure how to point to the age property and the result to return the name of the person.

var people = [
  { name : 'Matt', age: 19},
  { name : 'George', age: 16},
  { name : 'Ross', age: 12},
  { name : 'Gerard', age: 18},
  { name : 'Leo', age: 15},
  { name : 'Brad', age: 21}
 ];

The output in the console needs to look like:

George is 16 
Ross is 12
Leo is 15

Use filter. No need to make strings

 var people = [ { name: 'Matt', age: 19}, { name: 'George', age: 16}, { name: 'Ross', age: 12}, { name: 'Gerard', age: 18}, { name: 'Leo', age: 15}, { name: 'Brad', age: 21} ]; console.log(people.filter(function(e){return e.age<18}))

I think you can use ES6s filter method to archive this. But you need to polyfill the JS for it.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

        var people = [
      { name : 'Matt', age: 19},
      { name : 'George', age: 16},
      { name : 'Ross', age: 12},
      { name : 'Gerard', age: 18},
      { name : 'Leo', age: 15},
      { name : 'Brad', age: 21}
     ];
function isJung(p){
return p.age <18 ;
}
var youngPeople = people.filter(isJung);

And the polyfil is also provided by Mozilla:

if (!Array.prototype.filter){
  Array.prototype.filter = function(func, thisArg) {
    'use strict';
    if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
        throw new TypeError();

    var len = this.length >>> 0,
        res = new Array(len), // preallocate array
        t = this, c = 0, i = -1;

    var kValue;
    if (thisArg === undefined){
      while (++i !== len){
        // checks to see if the key was set
        if (i in this){
          kValue = t[i]; // in case t is changed in callback
          if (func(t[i], i, t)){
            res[c++] = kValue;
          }
        }
      }
    }
    else{
      while (++i !== len){
        // checks to see if the key was set
        if (i in this){
          kValue = t[i];
          if (func.call(thisArg, t[i], i, t)){
            res[c++] = kValue;
          }
        }
      }
    }

    res.length = c; // shrink down array to proper size
    return res;
  };
}

at first filter then map

 var people = [ { name: 'Matt', age: 19}, { name: 'George', age: 16}, { name: 'Ross', age: 12}, { name: 'Gerard', age: 18}, { name: 'Leo', age: 15}, { name: 'Brad', age: 21} ]; console.log(people.filter(function(e){return e.age<=18}).map(x => x.name+" is " +x.age));

use filter to keep the good ones and foreach to display.

const people = [
  { name : 'Matt', age: 19},
  { name : 'George', age: 16},
  { name : 'Ross', age: 12},
  { name : 'Gerard', age: 18},
  { name : 'Leo', age: 15},
  { name : 'Brad', age: 21}
 ];
people.filter(v => v.age < 18).forEach((v)=>{console.log(`${v.name} is ${v.age}`)})

Using ES6 javascript:

people.forEach((single) => {
  if(single.age < 18)
   console.log(`${single.name} is ${single.age}`);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM