小编luk*_*asz的帖子

foreach循环和返回undefined的值

我想知道是否有人能解释我为什么这个函数返回undefined而不是创建对象

var people = [
  {name: 'John'},
  {name: 'Dean'},
  {name: 'Jim'}
];

function test(name) {
  people.forEach(function(person){
    if (person.name === 'John') {
      return person;
    }   
  });
}

var john = test('John');
console.log(john);

// returning 'undefined'
Run Code Online (Sandbox Code Playgroud)

javascript foreach return-value undefined

3
推荐指数
1
解决办法
1103
查看次数

标签 统计

foreach ×1

javascript ×1

return-value ×1

undefined ×1