2016-09-17 15 views
-1

ers、 私はこのアルゴリズムに問題があります。2つのオブジェクトを返すJavascriptフィルタ

私はReduxを使用していますが、この問題には本当に関係しません。基本的には、このコードのconsole.logステートメントは、必要に応じてただ1つのオブジェクトを返しますが、関数Aは2つのオブジェクトの配列を返します(関数Cでテストに合格しなかった場合でも)。

Iそれを修正するのに役立つかどうかを確認するために関数を3つの部分に分けましたが、まだ分かりませんでした。

任意のアドバイスはありますか?

const A = (state) => { 
    // looks through an array and passes down a resource 
    return state.resources.locked.filter((resource) => { 
    return B(state, resource); 
    }) 
}; 
// looks through an array and passes down a building 
const B = (state, resource) => { 
    return state.bonfire.allStructures.filter((building) => { 
    return C(building, resource); 
    }) 
}; 
// checks if building name and resource requirment are the same, and if building is unlocked 
// then returns only that one 
const C = (building, resource) => { 
    if (building.unlocked && building.name == resource.requires.structure) { 
     console.log(resource); 
     return resource; 
    } 
} 
+1

:あなただけ評価される式を持って矢印の機能にショートカット表記を使用することができます

も注意してください。 –

答えて

1

filterを使用して、あなたはそれに渡すコールバック関数は、特定の要素内または除外する必要があるかどうかを示すブール値を返すことが期待されていることを実現します。

あなたのケースでは、Bはブール値ではなく配列を返します。その配列が空である(リソースが一致しないことを示す)場合でも、そのような値はfilterによって偽と見なされることはなく、対応するリソースはAによって返される配列内でも発生します。

簡単な修正:Bで返された配列の長さを取得し、代わりにそれを返します。ゼロは偽とみなされます。

const A = (state) => { 
 
    // looks through an array and passes down a resource 
 
    return state.resources.locked.filter((resource) => { 
 
    return B(state, resource).length; /// <---- length! 
 
    }) 
 
}; 
 
// looks through an array and passes down a building 
 
const B = (state, resource) => { 
 
    return state.bonfire.allStructures.filter((building) => { 
 
    return C(building, resource); 
 
    }) 
 
}; 
 
// checks if building name and resource requirement are the same, and if building 
 
// is unlocked and then returns only that one 
 
const C = (building, resource) => { 
 
    if (building.unlocked && building.name == resource.requires.structure) { 
 
     return resource; 
 
    } 
 
} 
 

 
// Sample data. Only x matches. 
 
var state = { 
 
    resources: { 
 
     locked: [{ // resource 
 
      requires: { 
 
       structure: 'x' 
 
      } 
 
     }, { // resource 
 
      requires: { 
 
       structure: 'y' 
 
      } 
 
     }] 
 
    }, 
 
    bonfire: { 
 
     allStructures: [{ // building 
 
      unlocked: true, 
 
      name: 'x' 
 
     }, { // building 
 
      unlocked: true, 
 
      name: 'z' 
 
     }] 
 
    } 
 
}; 
 

 
console.log(A(state));

しかし、より良いは、実際に彼らが期待されている各場所でブール値を返すようになります。したがってCは条件の結果を返すだけで、Bfilterの代わりにsomeを使用できます。これはブール値を返すだけでなく、一致が見つかるとさらに見えなくなります。 Aでは、実際にAが(ブール値ではなく)データを返すように、オリジナルのコードを作成することができます。サンプル `state`を提供してください

// looks through an array and passes down a resource 
 
const A = state => state.resources.locked.filter(resource => B(state, resource)); 
 
// looks through an array and passes down a building 
 
    // Use .some instead of .filter: it returns a boolean 
 
const B = (state, resource) => 
 
    state.bonfire.allStructures.some(building => C(building, resource)); 
 
// checks if building name and resource requirment are the same, and if building 
 
// is unlocked and then returns only that one 
 
    // Return boolean 
 
const C = (building, resource) => building.unlocked 
 
           && building.name == resource.requires.structure; 
 
// Sample data. Only x matches. 
 
var state = { 
 
    resources: { 
 
     locked: [{ // resource 
 
      requires: { 
 
       structure: 'x' 
 
      } 
 
     }, { // resource 
 
      requires: { 
 
       structure: 'y' 
 
      } 
 
     }] 
 
    }, 
 
    bonfire: { 
 
     allStructures: [{ // building 
 
      unlocked: true, 
 
      name: 'x' 
 
     }, { // building 
 
      unlocked: true, 
 
      name: 'z' 
 
     }] 
 
    } 
 
}; 
 
console.log(A(state));

関連する問題