0

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;
1
  • Not sure why this is closed. The question is clear. Commented Jan 1, 2020 at 16:07

2 Answers 2

4

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';
Sign up to request clarification or add additional context in comments.

1 Comment

This is the right answer. Even if IE doesn't natively support Array.inclides(..), this can be added to your polyfill file if Angular doesn't already include it.
0

Most of the browser support indexOf.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.