2017-09-13 4 views
-7

私は、次のコードを持っています。が正しく

サンプルコード:

var conditions = [{ 
 
     id: 14, 
 
     conditiontype: 1, 
 
     processsteptemplate: 9, 
 
     deleted: false, 
 
     processTemplate_id: 0 
 
    }, 
 
    { 
 
     id: 15, 
 
     conditiontype: 1, 
 
     processsteptemplate: 9, 
 
     deleted: false, 
 
     processTemplate_id: 0 
 
    }, 
 
    { 
 
     id: 16, 
 
     conditiontype: 1, 
 
     processsteptemplate: 10, 
 
     deleted: false, 
 
     processTemplate_id: 0 
 
    } 
 
    ], 
 
    stepId = 9; 
 
    
 
const output = conditions.slice(0).filter(condition => condition.processsteptemplate === stepId); 
 
console.log(output)
問題は===は、すべての要素が配列に残っているので、一致していないということです...

UPDATEに失敗マイいただきました!

+4

なぜ 'conditions.slice(0).filter'ですか? 'filter'はとにかく別の配列を返します。 – Rajesh

+0

スライスを削除するだけですか? – Felix

+1

'var output = conditions.filter(x => x.processsTemplate === stepId);を試してください。 console.log(出力) 'です。ポイントは、 'filter'は元の配列を決して操作しません。フィルタリングされた値のリストを返します – Rajesh

答えて

2

ここprocesssteptemplateの配列= 10は、おそらく間違って何をしていた

let arr = [ 
 
{id: 14, conditiontype: 1, processsteptemplate: 9, deleted: false, processTemplate_id: 0}, 
 
{id: 15, conditiontype: 1, processsteptemplate: 9, deleted: false, processTemplate_id: 0}, 
 
{id: 16, conditiontype: 1, processsteptemplate: 10, deleted: false, processTemplate_id: 0} 
 
] 
 

 
let step = 9; 
 

 
let result = arr.filter((e) => e.processsteptemplate === step); 
 

 
console.log(result);

を除去してあなたが

arr.filter((e) => e.processsteptemplate === step); 

を想定したことは、現在の配列の値を変更するというものでした、それが真ではない場合、今度はの結果に格納されている新しい配列を返します。上記のコードスニペット

+0

は、質問の編集を追加しました – marvel308

+0

正直言って、私はすでにこのコメントにコメントしました。また、このような質問は、ドキュメントを読むことで解決できます。だから、彼らに答える、何か、私のPOV、inn間違っている。質問に答えても、私はまだ投票を受けます。 – Rajesh