Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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
Javascript 如何解决jshint错误'Don';t在循环中生成函数。`with jquery$.grep_Javascript_Jquery_Loops_Jshint - Fatal编程技术网

Javascript 如何解决jshint错误'Don';t在循环中生成函数。`with jquery$.grep

Javascript 如何解决jshint错误'Don';t在循环中生成函数。`with jquery$.grep,javascript,jquery,loops,jshint,Javascript,Jquery,Loops,Jshint,下面的javascript代码为我提供了在循环中不生成函数的功能。错误 将函数置于循环之外: /* Get the favorite products from the data using the id */ productDataCategory[FAVORITES].length = 0/* empty favorites productDataCategory */; var f = function(e){ return e.id === currentUser.favorites[i

下面的javascript代码为我提供了在循环中不生成函数的功能。错误


将函数置于循环之外:

/* Get the favorite products from the data using the id */
productDataCategory[FAVORITES].length = 0/* empty favorites productDataCategory */;
var f = function(e){ return e.id === currentUser.favorites[i]; };
for ( i = 0; i < currentUser.favorites.length; i++) {
  product = $.grep(productData, f)[0];
  productDataCategory[FAVORITES].push(p);
}
/*使用id从数据中获取喜爱的产品*/
productDataCategory[FAVORITES].length=0/*空收藏夹productDataCategory*/;
var f=函数(e){返回e.id==currentUser.favorites[i];};
对于(i=0;i
工作起来很有魅力。(顺便说一句,我看到你编辑了你的答案!两个答案都有效,而且当前的答案非常简洁)为什么不只使用一个
.grep
(而不是循环几个答案),而是检查
$.inArray(e.id,currentUser.favorites)!==-1
?您是否需要按
currentUser.favorites
order重新排序结果数组?这是一个非常好的问题。答案是我从未听说过$.inArray。那会让事情变得更快!为每个产品使用.grep都需要很多时间!(我会说的)@raina77ow
。inArray
只给出位置。它是否一次处理多个位置?(放入一个完整的数组并获取所有位置)。如果没有,则意味着我需要
.inArray
每个产品+获取给定位置的数据
.grep
似乎是一个更好的解决方案!
{
    "user": "MBO",
    "email": "my@mail.com",
    "username": "Marcel",
    "photoURL": "url_here",
    "favorites": [13,25,40,56]
}
/* Get the favorite products from the data using the id */
productDataCategory[FAVORITES].length = 0/* empty favorites productDataCategory */;
var f = function(e){ return e.id === currentUser.favorites[i]; };
for ( i = 0; i < currentUser.favorites.length; i++) {
  product = $.grep(productData, f)[0];
  productDataCategory[FAVORITES].push(p);
}