Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在.each迭代中返回一个对象-JavaScript_Javascript_Object_Iteration - Fatal编程技术网

在.each迭代中返回一个对象-JavaScript

在.each迭代中返回一个对象-JavaScript,javascript,object,iteration,Javascript,Object,Iteration,我有一个名为Category的对象,它使用一个方法迭代产品数组(Product),并返回一个满足this.name==query条件的实例: function Category(products){ this.products = products; this.findProductByName = function(query){ $(this.products).each(function(){ return this.name === query;

我有一个名为
Category
的对象,它使用一个方法迭代产品数组(
Product
),并返回一个满足
this.name==query
条件的实例:

function Category(products){
  this.products = products; 
  this.findProductByName = function(query){
    $(this.products).each(function(){
      return this.name === query; 
    }
  }
}
我的
产品
(以防万一):

然后,我创建了一个带有产品的
类别
实例:

var $someCategory = new Category(
   [
      new Product('foo'),
      new Product('bar')
   ]
)`
当我打电话时:

$someCategory.findProductByName('foo'); // 'undefined'
尽管:

...
this.findProductByName = function(query){
  $(this.products).each(function(){
    console.log(this.name === query); // returns 'true'
  }
}
...

当满足
this.name==query
时,如何返回对象

您需要使用带有返回(或map/reduce)的传统循环,让函数返回匹配的结果。each函数对数组中的每个元素执行一个操作,它不会执行过滤,并忽略返回的值

试试这个:

this.findProductByName = function(query) {
  for (var i = 0; i < this.products.length; i++) {
    if (this.products[i].name === query)
    {
      return this.products[i];
    }
  }
}

您需要使用带有返回(或map/reduce)的传统循环,让函数返回匹配的结果。each函数对数组中的每个元素执行一个操作,它不会执行过滤,并忽略返回的值

试试这个:

this.findProductByName = function(query) {
  for (var i = 0; i < this.products.length; i++) {
    if (this.products[i].name === query)
    {
      return this.products[i];
    }
  }
}

您需要使用jQuery吗?您是否可以改为使用数组筛选器方法

function Category(products){
  this.products = products; 
  this.findProductByName = function(query){
    return this.products.filter(function(item){
      return item.name === query; 
    });
  };
}

您需要使用jQuery吗?您是否可以改为使用数组筛选器方法

function Category(products){
  this.products = products; 
  this.findProductByName = function(query){
    return this.products.filter(function(item){
      return item.name === query; 
    });
  };
}

$。each()
机制忽略返回的值(大部分)。您希望它做什么?使用[].filter()和[].map()而不是each()@Pointy在满足条件时返回正确的
Product
实例,以便我可以按名称搜索
$someCategory
中的产品。“忽略返回值”是什么意思?
$内部返回
。each()
回调仅从该函数返回;它不会从您的
findProductByName
函数返回。@Pointy ahhhh,有意义。谢谢
$。each()
机制忽略返回的值(大部分)。您希望它做什么?使用[].filter()和[].map()而不是each()@Pointy在满足条件时返回正确的
Product
实例,以便我可以按名称搜索
$someCategory
中的产品。“忽略返回值”是什么意思?
$内部返回
。each()
回调仅从该函数返回;它不会从您的
findProductByName
函数返回。@Pointy ahhhh,有意义。谢谢美好的谢谢问:那么,当for循环
已经处理迭代并返回VAL时,为什么还要使用
each
?当您明确希望对数组中的每个项执行操作时,即在name属性$(this.products)中添加前缀。each(function(i,el){el.name='My'+el.name;});还有一些人只是喜欢jquery而不是原生javascript。很好!谢谢问:那么,当for循环
已经处理迭代并返回VAL时,为什么还要使用
each
?当您明确希望对数组中的每个项执行操作时,即在name属性$(this.products)中添加前缀。each(function(i,el){el.name='My'+el.name;});还有一些人只是喜欢jquery而不是原生javascript。