Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to filter a javascript array with multiple matching conditions. The below code returns all the data, instead of just 2.

Code:

this.entityTypes = codeList.Values.filter(c => c.CodeValue === 'CRD_CRE_INS' || 'CRD_EEA_BRA');

Sample JSON Array:-

const codeList.Values = [{
    "CodeValue": "CRD_CRE_INS",
    "CodeValueDisplay": "CRD Credit Institution",
  },
  {
    "CodeValue": "CRD_EEA_BRA",
    "CodeValueDisplay": "EEA Branch",
  },
  {
    "CodeValue": "CRD_NON_EEA_BRA",
    "CodeValueDisplay": "Non-EEA Branch",
  }
] 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
140 views
Welcome To Ask or Share your Answers For Others

1 Answer

An || doesn't work that way

You could put the values in array instead. This makes it easily scale-able for number of conditions

this.entityTypes = codeList.Values
                   .filter(c => ['CRD_CRE_INS','CRD_EEA_BRA'].includes(c.CodeValue) );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...