2

次の JavaScript コードは、ループ内で関数を作成しないでください。エラー

/* Get the favorite products from the data using the id */
productDataCategory[FAVORITES].length = 0/* empty favorites productDataCategory */;
for ( i = 0; i < currentUser.favorites.length; i++) {
    product = $.grep(productData, function(e){ return e.id === currentUser.favorites[i]; })[0];
    productDataCategory[FAVORITES].push(p);
}

私は質問を調べて、他のメンバーから尋ねられたいくつかの同様の質問を見まし
た.jslintエラー「ループ内で関数を作成しないでください.」を回避する方法.
ループ内で関数を作成しないでください

私が抱えている問題は、ループ内で $.grep 関数を使用して配列内の製品を検索していることです。
上記の質問の回答でこの問題を解決する方法がわかりません。


ログインしたユーザーからのデータ

{
    "user": "MBO",
    "email": "my@mail.com",
    "username": "Marcel",
    "photoURL": "url_here",
    "favorites": [13,25,40,56]
}
4

1 に答える 1

2

関数をループの外に置きます。

/* 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);
}
于 2012-10-15T09:11:19.803 に答える