Umaiz Khan
Umaiz Khan

Reputation: 1227

Angular filter array by multiple strings

I have array i need to filter it by multiple values

data1 = [
  {
    "status": "60"
  },
  {
    "status": "70"
  }, 
  // so on
];

I am doing like this its working with single string not with multiple

var countsettled = this.data1.filter((obj) => obj.status === '60' || '61' || '62' || '63' || '64' || '75').length;
var countunsettled = this.data1.filter((obj) => obj.status === '71' || '72' || '73' || '74' || '31' || '32' || '33' || '34' || '66').length;

Upvotes: 0

Views: 610

Answers (2)

C.OG
C.OG

Reputation: 6529

You have to specify a property for each comparison:

var countsettled = this.data1.filter((obj) => obj.status === '60' || obj.status === '61' || obj.status === '62' || obj.status === '63' || obj.status === '64' || obj.status === '75').length;

An alternate way to do this more concisely will be to use Array.includes

var countsettledStatus = [60, 61, 62, 63, 64, 75];
var countsettled = this.data1.filter((obj) => countsettledStatus.includes(obj.status)).length;

IE11 Polyfill

Add the polyfill in your polyfill.ts file:

import 'core-js/es7/array';

Upvotes: 4

Sanoj_V
Sanoj_V

Reputation: 2986

Most of the browser support indexOf.

var countsettled = this.data1.filter(f=> ['60','61','62'].indexOf(f.status)!=-1).length;

Upvotes: 0

Related Questions