遍历数组中的数组以确定值

时间:2019-01-27 19:45:24

标签: javascript arrays loops

我需要使用for循环遍历以下数组,并确定年龄在18岁以上的人,还是未满18岁的人,并相应地记录字符串。

我一直在尝试一些类似的事情:

var peopleWhoWantToSeeMadMaxFuryRoad = [
  {
    name: "Mike",
    age: 12,
    gender: "male"
  },{
    name: "Madeline",
    age: 80,
    gender: "female"
  },{
    name: "Cheryl",
    age: 22,
    gender: "female"
  },{
    name: "Sam",
    age: 30,
    gender: "male"
  },{
    name: "Suzy",
    age: 4,
    gender: "female"
  }
] 


for(var i = 0; i < peopleWhoWantToSeeMadMaxFuryRoad.length; i++){
    for(var j = 0; j < peopleWhoWantToSeeMadMaxFuryRoad[i].length; j++){
        if (j == 'number' && j < 18){
            console.log("person is under 18")
        }
    }
}

但是我似乎无法理解。任何帮助将不胜感激

6 个答案:

答案 0 :(得分:3)

不需要双for循环,您可以使用一个for循环来实现:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.0.2/vue-router.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.22/vue.min.js"></script>
<div id="app">
  <router-view></router-view>
</div>

或者您可以使用foreach循环:

for(var i = 0; i < peopleWhoWantToSeeMadMaxFuryRoad.length; i++){
    if(peopleWhoWantToSeeMadMaxFuryRoad[i].age < 18) {
        // Do something for underaged people;
    } else {
       // Do something for people over 18
    }
}

答案 1 :(得分:0)

对象没有有意义的length。如果要提取age属性,请明确地执行此操作,例如obj.age

var peopleWhoWantToSeeMadMaxFuryRoad = [{
  name: "Mike",
  age: 12,
  gender: "male"
}, {
  name: "Madeline",
  age: 80,
  gender: "female"
}, {
  name: "Cheryl",
  age: 22,
  gender: "female"
}, {
  name: "Sam",
  age: 30,
  gender: "male"
}, {
  name: "Suzy",
  age: 4,
  gender: "female"
}]

for (var i = 0; i < peopleWhoWantToSeeMadMaxFuryRoad.length; i++) {
  if (peopleWhoWantToSeeMadMaxFuryRoad[i].age < 18) {
    console.log("person " + peopleWhoWantToSeeMadMaxFuryRoad[i].name + ' is under 18')
  }
}

或者,为了更加简洁,请使用数组方法并进行解构:

var peopleWhoWantToSeeMadMaxFuryRoad = [{
  name: "Mike",
  age: 12,
  gender: "male"
}, {
  name: "Madeline",
  age: 80,
  gender: "female"
}, {
  name: "Cheryl",
  age: 22,
  gender: "female"
}, {
  name: "Sam",
  age: 30,
  gender: "male"
}, {
  name: "Suzy",
  age: 4,
  gender: "female"
}]

peopleWhoWantToSeeMadMaxFuryRoad.forEach(({ name, age }) => {
  if (age < 18) {
    console.log("person " + name + ' is under 18');
  }
});

如果您想要一个包含每个年龄在18岁以下的人员对象的新数组,请使用.filter而不是.forEach

const under18 = peopleWhoWantToSeeMadMaxFuryRoad.filter(({ age}) => age < 18);

答案 2 :(得分:0)

代码中的问题是您没有正确访问值,j == 'number' && j < 18在这里是与索引而不是年龄进行比较

var data=[{name:"Mike",age:12,gender:"male"},{name:"Madeline",age:80,gender:"female"},{name:"Cheryl",age:22,gender:"female"},{name:"Sam",age:30,gender:"male"},{name:"Suzy",age:4,gender:"female"}]


data.forEach(e=> {
  if(e.age >= 18 ) {
    console.log( e.name + " you're 18+" )
  } else {
    console.log( e.name + " Below 18")
  }
})

答案 3 :(得分:0)

使用forEach

var peopleWhoWantToSeeMadMaxFuryRoad = [
  {
    name: "Mike",
    age: 12,
    gender: "male"
  },{
    name: "Madeline",
    age: 80,
    gender: "female"
  },{
    name: "Cheryl",
    age: 22,
    gender: "female"
  },{
    name: "Sam",
    age: 30,
    gender: "male"
  },{
    name: "Suzy",
    age: 4,
    gender: "female"
  }
] 
peopleWhoWantToSeeMadMaxFuryRoad.forEach((e)=>e.age<18?console.log(e.name + ' is underage'):true)

答案 4 :(得分:0)

您可以将一个布尔值映射到每个人,按布尔值过滤,然后遍历其余数组以进行记录。

var peopleWhoWantToSeeMadMaxFuryRoad = [
  {
    name: "Mike",
    age: 12,
    gender: "male"
  },{
    name: "Madeline",
    age: 80,
    gender: "female"
  },{
    name: "Cheryl",
    age: 22,
    gender: "female"
  },{
    name: "Sam",
    age: 30,
    gender: "male"
  },{
    name: "Suzy",
    age: 4,
    gender: "female"
  }
];

var under18 = peopleWhoWantToSeeMadMaxFuryRoad.map((peeps)={
    peeps["under18"] = (peeps.age < 18);
    return peeps;
}).filter((peeps)=>{
    return peeps.uner18;
}).forEach((peeps)=>{
    console.log(`${peeps.name} is under 18`);
})

答案 5 :(得分:-1)

建议是使用Array Filter清除所有18个或以上的内容,然后循环其余的内容并打印console.log。

const peopleWhoWantToSeeMadMaxFuryRoad = [..];

peopleWhoWantToSeeMadMaxFuryRoad.filter(person => person.age < 18)
    .forEach(person => console.log(`${person.name} is under 18`));