关于javascript:使用函数和参数迭代包含对象的数组

Iterating through an array containing objects using a function and a parameter

我想创建一个函数,它遍历一个数组,该数组包含具有名称、姓氏和年龄属性的独立对象。函数应采用参数"name",并在控制台中返回名称、姓氏和年龄。

我该怎么做?我看过其他一些问题,但似乎不知道该怎么做。到目前为止,我已经创造了…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var people = [
  { name:"Sue", surname:"Beckett", age: 50},
  { name:"Bill", surname:"Borough", age: 44},
  { name:"Janet", surname:"Jupp", age: 23},
  { name:"Peter", surname:"Pepper", age: 21},
  { name:"Samantha", surname:"Salad", age: 17}
];


function person(name) {
  // code here
}

person(sue)


简单使用EDOCX1&0

ZZU1

Demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var people = [{
    name:"Sue",
    surname:"Beckett",
    age: 50
  },
  {
    name:"Bill",
    surname:"Borough",
    age: 44
  },
  {
    name:"Janet",
    surname:"Jupp",
    age: 23
  },
  {
    name:"Peter",
    surname:"Pepper",
    age: 21
  },
  {
    name:"Samantha",
    surname:"Salad",
    age: 17
  }
];

function person(name) {
  return people.find(function(p) {
    return p.name == name;
  });
}
console.log(person("Samantha"));
console.log(Object.values(person("Samantha")));

注:

  • 当你呼唤你的方法时,如果没有""的话,你会认为你有一个叫sue的变量。
  • 说明你可以使用EDOCX1&5The object properties,instead of the whole object.

Simply write:

1
console.log(Object.values(person("Samantha")));

你可以通过检查EDOCX1&7&Property and EDOCX1&7&Variable来过滤阵列。

1
2
3
4
5
6
7
8
9
10
11
12
13
function person(name) {
    people
        .filter(o => o.name.toLowerCase() === name.toLowerCase())
        .forEach(({ name, surname, age}) => {
            console.log('name:', name);
            console.log('suname:', surname);
            console.log('age:', age);
        });
}

var people = [{ name:"Sue", surname:"Beckett", age: 50 }, { name:"Bill", surname:"Borough", age: 44 }, { name:"Janet", surname:"Jupp", age: 23 }, { name:"Peter", surname:"Pepper", age: 21 }, { name:"Samantha", surname:"Salad", age: 17 }];

person('sue');


使用filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var people = [
    { name:"Sue", surname:"Beckett", age: 50},
    { name:"Bill", surname:"Borough", age: 44},
    { name:"Janet", surname:"Jupp", age: 23},
    { name:"Peter", surname:"Pepper", age: 21},
    { name:"Samantha", surname:"Salad", age: 17}
];


function person(personName) {
    var user = people.filter(function (user) {
       return (user.name.toLowerCase() == personName.toLowerCase());
    });
    return user[0] !== undefined ? user[0] : 'not found';
}


console.log(person('sue'));


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var people = [
  { name:"Sue", surname:"Beckett", age: 50},
  { name:"Bill", surname:"Borough", age: 44},
  { name:"Janet", surname:"Jupp", age: 23},
  { name:"Peter", surname:"Pepper", age: 21},
  { name:"Samantha", surname:"Salad", age: 17}
];
   
   
function person(name) {
  for (var key in people) {
    var person = people[key];
    if (name === person.name) {
      console.log(person);
    }
  }
}

person("Sue"); // string always needs quotes:" or ' character


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var people = [
  { name:"Sue", surname:"Beckett", age: 50},
  { name:"Bill", surname:"Borough", age: 44},
  { name:"Janet", surname:"Jupp", age: 23},
  { name:"Peter", surname:"Pepper", age: 21},
  { name:"Samantha", surname:"Salad", age: 17}
];


function person(name) {
  people.map((data,index)=>{
    if(data.name == name)
    {
      console.log(data.name);
      console.log(data.surname);
      console.log(data.age);
      return;
    }
  })
}

person("Sue");