3

I have an array as:

var arr = 
[
    {
        "id":3,
        "first_name":"Laila",
        "last_name":"McCaine",
        "gender":"female",      
        "populations":[{"population_name":"Heart failure"}, {"population_name":"AMI"}],
        "score": 55.0
    },
    {
        "id":5,     
        "first_name":"Riva",
        "last_name":"Rontgen",
        "gender":"female",
        "populations":[{"population_name":"Pnumonia"}],
        "score": 85.0
    },
    {
        "id":8,
        "first_name":"Emily",   
        "last_name":"Rosewood",
        "gender":"female",  
        "populations":[],
        "score": 25.0
    }
];

and variables :

var score='';
var population='';
var status = '';

I want to use array filter function with and conditions such as Get records having score less than 40, status = 1 and population_name as "heart failure".

The problem is, given 3 variables are dynamic and filter should not be applied if its value is ''.

How can I achieve this?

2
  • Do you want to apply array filter automatically every time the array gets modified? Commented Apr 27, 2016 at 12:13
  • Well, it's quite straight-forward to check whether the filters are ""; what have you tried? Commented Apr 27, 2016 at 12:14

2 Answers 2

10

Something like this:

arr.filter(function(item) {
    var ok = true;

    if (score !== '') {
        ok = item.score < score;
    }

    if (ok && population !== '') {
        // check it
        ok = item.populations.map(function() {return item.population_name;}).indexOf(population) > -1;
    }

    if (ok && status !== '') {
        // check status
    }

    return ok;
});
Sign up to request clarification or add additional context in comments.

Comments

0

var arr = 
[
    {
        "id":3,
        "status": 1,
        "first_name":"Laila",
        "last_name":"McCaine",
        "gender":"female",      
        "populations":[{"population_name":"Heart failure"}, {"population_name":"AMI"}],
        "score": 55.0
    },
    {
        "id":5,   
        "status": 1,
        "first_name":"Riva",
        "last_name":"Rontgen",
        "gender":"female",
        "populations":[{"population_name":"Pnumonia"}],
        "score": 85.0
    },
    {
        "id":8,
        "status": 0,
        "first_name":"Emily",   
        "last_name":"Rosewood",
        "gender":"female",  
        "populations":[],
        "score": 25.0
    }
];

function filterRecords( arr ){
  return arr.filter(function( record ){
    return record.score > 40 && record.status && (record.populations.filter(function( population ){
      return population.population_name == 'Heart failure';
    })).length;
  });
}

console.log( filterRecords( arr ) );
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

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.