How to use filter with conditionals inside?











up vote
1
down vote

favorite
2












I tried apply chain of conditionals inside `filter. How to join result from previous conditionals and pass result the next:



this.fltRooms = this.rooms.filter(room => {
let _rule;
if (formValues["auditoryNumber"]) {
_rule = formValues["auditoryNumber"] == room.title;
}

if (formValues["floor"]) {
_rule = formValues["floor"] == room.floor; // Should be AND
}

if (formValues["corpus"]) {
_rule = formValues["corpus"] == room.building; // Should be AND
}

return _rule;
});


So, logic is, if exist formValues["auditoryNumber"] and formValues["floor"] and formValues["corpus"] then filter by all three conditionals with AND between.










share|improve this question






















  • also would great to provide sample array flitrooms
    – dganenco
    Nov 19 at 14:28










  • Is it possible that no filters are set? And if no filters set should it return all or none?
    – epascarello
    Nov 19 at 14:32












  • Seems like all you need is _rule = _rule && ... and also initialize _rule to true
    – Pointy
    Nov 19 at 14:32















up vote
1
down vote

favorite
2












I tried apply chain of conditionals inside `filter. How to join result from previous conditionals and pass result the next:



this.fltRooms = this.rooms.filter(room => {
let _rule;
if (formValues["auditoryNumber"]) {
_rule = formValues["auditoryNumber"] == room.title;
}

if (formValues["floor"]) {
_rule = formValues["floor"] == room.floor; // Should be AND
}

if (formValues["corpus"]) {
_rule = formValues["corpus"] == room.building; // Should be AND
}

return _rule;
});


So, logic is, if exist formValues["auditoryNumber"] and formValues["floor"] and formValues["corpus"] then filter by all three conditionals with AND between.










share|improve this question






















  • also would great to provide sample array flitrooms
    – dganenco
    Nov 19 at 14:28










  • Is it possible that no filters are set? And if no filters set should it return all or none?
    – epascarello
    Nov 19 at 14:32












  • Seems like all you need is _rule = _rule && ... and also initialize _rule to true
    – Pointy
    Nov 19 at 14:32













up vote
1
down vote

favorite
2









up vote
1
down vote

favorite
2






2





I tried apply chain of conditionals inside `filter. How to join result from previous conditionals and pass result the next:



this.fltRooms = this.rooms.filter(room => {
let _rule;
if (formValues["auditoryNumber"]) {
_rule = formValues["auditoryNumber"] == room.title;
}

if (formValues["floor"]) {
_rule = formValues["floor"] == room.floor; // Should be AND
}

if (formValues["corpus"]) {
_rule = formValues["corpus"] == room.building; // Should be AND
}

return _rule;
});


So, logic is, if exist formValues["auditoryNumber"] and formValues["floor"] and formValues["corpus"] then filter by all three conditionals with AND between.










share|improve this question













I tried apply chain of conditionals inside `filter. How to join result from previous conditionals and pass result the next:



this.fltRooms = this.rooms.filter(room => {
let _rule;
if (formValues["auditoryNumber"]) {
_rule = formValues["auditoryNumber"] == room.title;
}

if (formValues["floor"]) {
_rule = formValues["floor"] == room.floor; // Should be AND
}

if (formValues["corpus"]) {
_rule = formValues["corpus"] == room.building; // Should be AND
}

return _rule;
});


So, logic is, if exist formValues["auditoryNumber"] and formValues["floor"] and formValues["corpus"] then filter by all three conditionals with AND between.







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 14:20









OPV

1,35521329




1,35521329












  • also would great to provide sample array flitrooms
    – dganenco
    Nov 19 at 14:28










  • Is it possible that no filters are set? And if no filters set should it return all or none?
    – epascarello
    Nov 19 at 14:32












  • Seems like all you need is _rule = _rule && ... and also initialize _rule to true
    – Pointy
    Nov 19 at 14:32


















  • also would great to provide sample array flitrooms
    – dganenco
    Nov 19 at 14:28










  • Is it possible that no filters are set? And if no filters set should it return all or none?
    – epascarello
    Nov 19 at 14:32












  • Seems like all you need is _rule = _rule && ... and also initialize _rule to true
    – Pointy
    Nov 19 at 14:32
















also would great to provide sample array flitrooms
– dganenco
Nov 19 at 14:28




also would great to provide sample array flitrooms
– dganenco
Nov 19 at 14:28












Is it possible that no filters are set? And if no filters set should it return all or none?
– epascarello
Nov 19 at 14:32






Is it possible that no filters are set? And if no filters set should it return all or none?
– epascarello
Nov 19 at 14:32














Seems like all you need is _rule = _rule && ... and also initialize _rule to true
– Pointy
Nov 19 at 14:32




Seems like all you need is _rule = _rule && ... and also initialize _rule to true
– Pointy
Nov 19 at 14:32












5 Answers
5






active

oldest

votes

















up vote
2
down vote



accepted










Basically you want to say if the filter is not set that it is true. If the filter is set than check out if the value matches.



this.fltRooms = this.rooms.filter(room => 
(!formValues["auditoryNumber"] || formValues["auditoryNumber"] == room.title) &&
(!formValues["floor"] || formValues["floor"] == room.floor) &&
(!formValues["corpus"] || formValues["corpus"] == room.building)
)


Other way with ifs would be using the boolean in each check



this.fltRooms = this.rooms.filter(room => {
let _rule = true
if (formValues["auditoryNumber"]) {
_rule = formValues["auditoryNumber"] == room.title;
}

if (_rule && formValues["floor"]) {
_rule = formValues["floor"] == room.floor; // Should be AND
}

if (_rule && formValues["corpus"]) {
_rule = formValues["corpus"] == room.building; // Should be AND
}

return _rule;
});





share|improve this answer





















  • I prefer second way with boolean, thnak you
    – OPV
    Nov 19 at 14:55


















up vote
3
down vote













And what if either doesn't exist?



As you describe it, it should be quite straightforward. Something like



this.fltRooms = this.rooms.filter(room =>
formValues["auditoryNumber"] == room.title
&& formValues["floor"] == room.floor
&& formValues["corpus"] == room.building);





share|improve this answer

















  • 1




    not exactly what OP is after it appears.
    – epascarello
    Nov 19 at 14:29


















up vote
2
down vote













Alternative to @bipll correct answer, using a defined array of keys to compare



this.filtRooms = this.rooms.filter(x => [
'auditoryNumber',
'floor',
'corpus',
].every(y => x[y] === formValues[y]));




If keys are different on both objects to compare



this.filtRooms = this.rooms.filter(x => [
[
'auditoryNumber',
'title',
],
[
'floor',
'floor',
],
[
'corpus',
'building',
],
].every(([
key1,
key2,
]) => x[key1] === void 0 || x[key1] === formValues[key2]));





share|improve this answer



















  • 1




    keys aren't same in each object
    – charlietfl
    Nov 19 at 14:31












  • @charlietfl good point! Didn't saw it, add a new example
    – Grégory NEUT
    Nov 19 at 14:33










  • @charlietfl If people are reading this : use every if you want all keys to match, use some if you want only one key to match. I think OP want all keys to match
    – Grégory NEUT
    Nov 19 at 14:41








  • 1




    re-reading the key needs to exist in formValues to be considered as filter. So what should also check in every is if it exists. Not well explained in question
    – charlietfl
    Nov 19 at 14:45












  • @charlietfl updated!
    – Grégory NEUT
    Nov 19 at 14:54


















up vote
0
down vote













this.filterRooms = this.rooms.filter(meetsSearchParams);

function meetsSearchParams(room){
return room.auditoryNumber == formValues["auditoryNumber"] &&
room.floor == formValues["floor"] &&
room.corpus == formValues["corpus"];
}


For the sake of keeping this as readable as possible.






share|improve this answer





















  • I will not work, cause I dont have formValues parameters all time
    – OPV
    Nov 19 at 15:04










  • Oh sorry I misread that in your question then. Thought you were just trying to check multiple conditions inside a filter. Are the inputs always there, just they don't necessarily have value?
    – Katie.Sun
    Nov 19 at 15:06




















up vote
0
down vote













If you're going to need to extend it, make a map array between the two structures



var fields = [
{f: "auditoryNumber", p: "title"},
{f: "floor", p: "floor"},
{f: "corpus", p: "building"}
];

this.fltRooms = this.rooms.filter(room =>
fields.every(field => !formValues[field.f] || formValues[field.f] == room[field.p]);
)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376611%2fhow-to-use-filter-with-conditionals-inside%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Basically you want to say if the filter is not set that it is true. If the filter is set than check out if the value matches.



    this.fltRooms = this.rooms.filter(room => 
    (!formValues["auditoryNumber"] || formValues["auditoryNumber"] == room.title) &&
    (!formValues["floor"] || formValues["floor"] == room.floor) &&
    (!formValues["corpus"] || formValues["corpus"] == room.building)
    )


    Other way with ifs would be using the boolean in each check



    this.fltRooms = this.rooms.filter(room => {
    let _rule = true
    if (formValues["auditoryNumber"]) {
    _rule = formValues["auditoryNumber"] == room.title;
    }

    if (_rule && formValues["floor"]) {
    _rule = formValues["floor"] == room.floor; // Should be AND
    }

    if (_rule && formValues["corpus"]) {
    _rule = formValues["corpus"] == room.building; // Should be AND
    }

    return _rule;
    });





    share|improve this answer





















    • I prefer second way with boolean, thnak you
      – OPV
      Nov 19 at 14:55















    up vote
    2
    down vote



    accepted










    Basically you want to say if the filter is not set that it is true. If the filter is set than check out if the value matches.



    this.fltRooms = this.rooms.filter(room => 
    (!formValues["auditoryNumber"] || formValues["auditoryNumber"] == room.title) &&
    (!formValues["floor"] || formValues["floor"] == room.floor) &&
    (!formValues["corpus"] || formValues["corpus"] == room.building)
    )


    Other way with ifs would be using the boolean in each check



    this.fltRooms = this.rooms.filter(room => {
    let _rule = true
    if (formValues["auditoryNumber"]) {
    _rule = formValues["auditoryNumber"] == room.title;
    }

    if (_rule && formValues["floor"]) {
    _rule = formValues["floor"] == room.floor; // Should be AND
    }

    if (_rule && formValues["corpus"]) {
    _rule = formValues["corpus"] == room.building; // Should be AND
    }

    return _rule;
    });





    share|improve this answer





















    • I prefer second way with boolean, thnak you
      – OPV
      Nov 19 at 14:55













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Basically you want to say if the filter is not set that it is true. If the filter is set than check out if the value matches.



    this.fltRooms = this.rooms.filter(room => 
    (!formValues["auditoryNumber"] || formValues["auditoryNumber"] == room.title) &&
    (!formValues["floor"] || formValues["floor"] == room.floor) &&
    (!formValues["corpus"] || formValues["corpus"] == room.building)
    )


    Other way with ifs would be using the boolean in each check



    this.fltRooms = this.rooms.filter(room => {
    let _rule = true
    if (formValues["auditoryNumber"]) {
    _rule = formValues["auditoryNumber"] == room.title;
    }

    if (_rule && formValues["floor"]) {
    _rule = formValues["floor"] == room.floor; // Should be AND
    }

    if (_rule && formValues["corpus"]) {
    _rule = formValues["corpus"] == room.building; // Should be AND
    }

    return _rule;
    });





    share|improve this answer












    Basically you want to say if the filter is not set that it is true. If the filter is set than check out if the value matches.



    this.fltRooms = this.rooms.filter(room => 
    (!formValues["auditoryNumber"] || formValues["auditoryNumber"] == room.title) &&
    (!formValues["floor"] || formValues["floor"] == room.floor) &&
    (!formValues["corpus"] || formValues["corpus"] == room.building)
    )


    Other way with ifs would be using the boolean in each check



    this.fltRooms = this.rooms.filter(room => {
    let _rule = true
    if (formValues["auditoryNumber"]) {
    _rule = formValues["auditoryNumber"] == room.title;
    }

    if (_rule && formValues["floor"]) {
    _rule = formValues["floor"] == room.floor; // Should be AND
    }

    if (_rule && formValues["corpus"]) {
    _rule = formValues["corpus"] == room.building; // Should be AND
    }

    return _rule;
    });






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 14:39









    epascarello

    150k13131178




    150k13131178












    • I prefer second way with boolean, thnak you
      – OPV
      Nov 19 at 14:55


















    • I prefer second way with boolean, thnak you
      – OPV
      Nov 19 at 14:55
















    I prefer second way with boolean, thnak you
    – OPV
    Nov 19 at 14:55




    I prefer second way with boolean, thnak you
    – OPV
    Nov 19 at 14:55












    up vote
    3
    down vote













    And what if either doesn't exist?



    As you describe it, it should be quite straightforward. Something like



    this.fltRooms = this.rooms.filter(room =>
    formValues["auditoryNumber"] == room.title
    && formValues["floor"] == room.floor
    && formValues["corpus"] == room.building);





    share|improve this answer

















    • 1




      not exactly what OP is after it appears.
      – epascarello
      Nov 19 at 14:29















    up vote
    3
    down vote













    And what if either doesn't exist?



    As you describe it, it should be quite straightforward. Something like



    this.fltRooms = this.rooms.filter(room =>
    formValues["auditoryNumber"] == room.title
    && formValues["floor"] == room.floor
    && formValues["corpus"] == room.building);





    share|improve this answer

















    • 1




      not exactly what OP is after it appears.
      – epascarello
      Nov 19 at 14:29













    up vote
    3
    down vote










    up vote
    3
    down vote









    And what if either doesn't exist?



    As you describe it, it should be quite straightforward. Something like



    this.fltRooms = this.rooms.filter(room =>
    formValues["auditoryNumber"] == room.title
    && formValues["floor"] == room.floor
    && formValues["corpus"] == room.building);





    share|improve this answer












    And what if either doesn't exist?



    As you describe it, it should be quite straightforward. Something like



    this.fltRooms = this.rooms.filter(room =>
    formValues["auditoryNumber"] == room.title
    && formValues["floor"] == room.floor
    && formValues["corpus"] == room.building);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 14:25









    bipll

    7,7991825




    7,7991825








    • 1




      not exactly what OP is after it appears.
      – epascarello
      Nov 19 at 14:29














    • 1




      not exactly what OP is after it appears.
      – epascarello
      Nov 19 at 14:29








    1




    1




    not exactly what OP is after it appears.
    – epascarello
    Nov 19 at 14:29




    not exactly what OP is after it appears.
    – epascarello
    Nov 19 at 14:29










    up vote
    2
    down vote













    Alternative to @bipll correct answer, using a defined array of keys to compare



    this.filtRooms = this.rooms.filter(x => [
    'auditoryNumber',
    'floor',
    'corpus',
    ].every(y => x[y] === formValues[y]));




    If keys are different on both objects to compare



    this.filtRooms = this.rooms.filter(x => [
    [
    'auditoryNumber',
    'title',
    ],
    [
    'floor',
    'floor',
    ],
    [
    'corpus',
    'building',
    ],
    ].every(([
    key1,
    key2,
    ]) => x[key1] === void 0 || x[key1] === formValues[key2]));





    share|improve this answer



















    • 1




      keys aren't same in each object
      – charlietfl
      Nov 19 at 14:31












    • @charlietfl good point! Didn't saw it, add a new example
      – Grégory NEUT
      Nov 19 at 14:33










    • @charlietfl If people are reading this : use every if you want all keys to match, use some if you want only one key to match. I think OP want all keys to match
      – Grégory NEUT
      Nov 19 at 14:41








    • 1




      re-reading the key needs to exist in formValues to be considered as filter. So what should also check in every is if it exists. Not well explained in question
      – charlietfl
      Nov 19 at 14:45












    • @charlietfl updated!
      – Grégory NEUT
      Nov 19 at 14:54















    up vote
    2
    down vote













    Alternative to @bipll correct answer, using a defined array of keys to compare



    this.filtRooms = this.rooms.filter(x => [
    'auditoryNumber',
    'floor',
    'corpus',
    ].every(y => x[y] === formValues[y]));




    If keys are different on both objects to compare



    this.filtRooms = this.rooms.filter(x => [
    [
    'auditoryNumber',
    'title',
    ],
    [
    'floor',
    'floor',
    ],
    [
    'corpus',
    'building',
    ],
    ].every(([
    key1,
    key2,
    ]) => x[key1] === void 0 || x[key1] === formValues[key2]));





    share|improve this answer



















    • 1




      keys aren't same in each object
      – charlietfl
      Nov 19 at 14:31












    • @charlietfl good point! Didn't saw it, add a new example
      – Grégory NEUT
      Nov 19 at 14:33










    • @charlietfl If people are reading this : use every if you want all keys to match, use some if you want only one key to match. I think OP want all keys to match
      – Grégory NEUT
      Nov 19 at 14:41








    • 1




      re-reading the key needs to exist in formValues to be considered as filter. So what should also check in every is if it exists. Not well explained in question
      – charlietfl
      Nov 19 at 14:45












    • @charlietfl updated!
      – Grégory NEUT
      Nov 19 at 14:54













    up vote
    2
    down vote










    up vote
    2
    down vote









    Alternative to @bipll correct answer, using a defined array of keys to compare



    this.filtRooms = this.rooms.filter(x => [
    'auditoryNumber',
    'floor',
    'corpus',
    ].every(y => x[y] === formValues[y]));




    If keys are different on both objects to compare



    this.filtRooms = this.rooms.filter(x => [
    [
    'auditoryNumber',
    'title',
    ],
    [
    'floor',
    'floor',
    ],
    [
    'corpus',
    'building',
    ],
    ].every(([
    key1,
    key2,
    ]) => x[key1] === void 0 || x[key1] === formValues[key2]));





    share|improve this answer














    Alternative to @bipll correct answer, using a defined array of keys to compare



    this.filtRooms = this.rooms.filter(x => [
    'auditoryNumber',
    'floor',
    'corpus',
    ].every(y => x[y] === formValues[y]));




    If keys are different on both objects to compare



    this.filtRooms = this.rooms.filter(x => [
    [
    'auditoryNumber',
    'title',
    ],
    [
    'floor',
    'floor',
    ],
    [
    'corpus',
    'building',
    ],
    ].every(([
    key1,
    key2,
    ]) => x[key1] === void 0 || x[key1] === formValues[key2]));






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 at 14:54

























    answered Nov 19 at 14:30









    Grégory NEUT

    8,19121437




    8,19121437








    • 1




      keys aren't same in each object
      – charlietfl
      Nov 19 at 14:31












    • @charlietfl good point! Didn't saw it, add a new example
      – Grégory NEUT
      Nov 19 at 14:33










    • @charlietfl If people are reading this : use every if you want all keys to match, use some if you want only one key to match. I think OP want all keys to match
      – Grégory NEUT
      Nov 19 at 14:41








    • 1




      re-reading the key needs to exist in formValues to be considered as filter. So what should also check in every is if it exists. Not well explained in question
      – charlietfl
      Nov 19 at 14:45












    • @charlietfl updated!
      – Grégory NEUT
      Nov 19 at 14:54














    • 1




      keys aren't same in each object
      – charlietfl
      Nov 19 at 14:31












    • @charlietfl good point! Didn't saw it, add a new example
      – Grégory NEUT
      Nov 19 at 14:33










    • @charlietfl If people are reading this : use every if you want all keys to match, use some if you want only one key to match. I think OP want all keys to match
      – Grégory NEUT
      Nov 19 at 14:41








    • 1




      re-reading the key needs to exist in formValues to be considered as filter. So what should also check in every is if it exists. Not well explained in question
      – charlietfl
      Nov 19 at 14:45












    • @charlietfl updated!
      – Grégory NEUT
      Nov 19 at 14:54








    1




    1




    keys aren't same in each object
    – charlietfl
    Nov 19 at 14:31






    keys aren't same in each object
    – charlietfl
    Nov 19 at 14:31














    @charlietfl good point! Didn't saw it, add a new example
    – Grégory NEUT
    Nov 19 at 14:33




    @charlietfl good point! Didn't saw it, add a new example
    – Grégory NEUT
    Nov 19 at 14:33












    @charlietfl If people are reading this : use every if you want all keys to match, use some if you want only one key to match. I think OP want all keys to match
    – Grégory NEUT
    Nov 19 at 14:41






    @charlietfl If people are reading this : use every if you want all keys to match, use some if you want only one key to match. I think OP want all keys to match
    – Grégory NEUT
    Nov 19 at 14:41






    1




    1




    re-reading the key needs to exist in formValues to be considered as filter. So what should also check in every is if it exists. Not well explained in question
    – charlietfl
    Nov 19 at 14:45






    re-reading the key needs to exist in formValues to be considered as filter. So what should also check in every is if it exists. Not well explained in question
    – charlietfl
    Nov 19 at 14:45














    @charlietfl updated!
    – Grégory NEUT
    Nov 19 at 14:54




    @charlietfl updated!
    – Grégory NEUT
    Nov 19 at 14:54










    up vote
    0
    down vote













    this.filterRooms = this.rooms.filter(meetsSearchParams);

    function meetsSearchParams(room){
    return room.auditoryNumber == formValues["auditoryNumber"] &&
    room.floor == formValues["floor"] &&
    room.corpus == formValues["corpus"];
    }


    For the sake of keeping this as readable as possible.






    share|improve this answer





















    • I will not work, cause I dont have formValues parameters all time
      – OPV
      Nov 19 at 15:04










    • Oh sorry I misread that in your question then. Thought you were just trying to check multiple conditions inside a filter. Are the inputs always there, just they don't necessarily have value?
      – Katie.Sun
      Nov 19 at 15:06

















    up vote
    0
    down vote













    this.filterRooms = this.rooms.filter(meetsSearchParams);

    function meetsSearchParams(room){
    return room.auditoryNumber == formValues["auditoryNumber"] &&
    room.floor == formValues["floor"] &&
    room.corpus == formValues["corpus"];
    }


    For the sake of keeping this as readable as possible.






    share|improve this answer





















    • I will not work, cause I dont have formValues parameters all time
      – OPV
      Nov 19 at 15:04










    • Oh sorry I misread that in your question then. Thought you were just trying to check multiple conditions inside a filter. Are the inputs always there, just they don't necessarily have value?
      – Katie.Sun
      Nov 19 at 15:06















    up vote
    0
    down vote










    up vote
    0
    down vote









    this.filterRooms = this.rooms.filter(meetsSearchParams);

    function meetsSearchParams(room){
    return room.auditoryNumber == formValues["auditoryNumber"] &&
    room.floor == formValues["floor"] &&
    room.corpus == formValues["corpus"];
    }


    For the sake of keeping this as readable as possible.






    share|improve this answer












    this.filterRooms = this.rooms.filter(meetsSearchParams);

    function meetsSearchParams(room){
    return room.auditoryNumber == formValues["auditoryNumber"] &&
    room.floor == formValues["floor"] &&
    room.corpus == formValues["corpus"];
    }


    For the sake of keeping this as readable as possible.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 14:40









    Katie.Sun

    55713




    55713












    • I will not work, cause I dont have formValues parameters all time
      – OPV
      Nov 19 at 15:04










    • Oh sorry I misread that in your question then. Thought you were just trying to check multiple conditions inside a filter. Are the inputs always there, just they don't necessarily have value?
      – Katie.Sun
      Nov 19 at 15:06




















    • I will not work, cause I dont have formValues parameters all time
      – OPV
      Nov 19 at 15:04










    • Oh sorry I misread that in your question then. Thought you were just trying to check multiple conditions inside a filter. Are the inputs always there, just they don't necessarily have value?
      – Katie.Sun
      Nov 19 at 15:06


















    I will not work, cause I dont have formValues parameters all time
    – OPV
    Nov 19 at 15:04




    I will not work, cause I dont have formValues parameters all time
    – OPV
    Nov 19 at 15:04












    Oh sorry I misread that in your question then. Thought you were just trying to check multiple conditions inside a filter. Are the inputs always there, just they don't necessarily have value?
    – Katie.Sun
    Nov 19 at 15:06






    Oh sorry I misread that in your question then. Thought you were just trying to check multiple conditions inside a filter. Are the inputs always there, just they don't necessarily have value?
    – Katie.Sun
    Nov 19 at 15:06












    up vote
    0
    down vote













    If you're going to need to extend it, make a map array between the two structures



    var fields = [
    {f: "auditoryNumber", p: "title"},
    {f: "floor", p: "floor"},
    {f: "corpus", p: "building"}
    ];

    this.fltRooms = this.rooms.filter(room =>
    fields.every(field => !formValues[field.f] || formValues[field.f] == room[field.p]);
    )





    share|improve this answer

























      up vote
      0
      down vote













      If you're going to need to extend it, make a map array between the two structures



      var fields = [
      {f: "auditoryNumber", p: "title"},
      {f: "floor", p: "floor"},
      {f: "corpus", p: "building"}
      ];

      this.fltRooms = this.rooms.filter(room =>
      fields.every(field => !formValues[field.f] || formValues[field.f] == room[field.p]);
      )





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        If you're going to need to extend it, make a map array between the two structures



        var fields = [
        {f: "auditoryNumber", p: "title"},
        {f: "floor", p: "floor"},
        {f: "corpus", p: "building"}
        ];

        this.fltRooms = this.rooms.filter(room =>
        fields.every(field => !formValues[field.f] || formValues[field.f] == room[field.p]);
        )





        share|improve this answer












        If you're going to need to extend it, make a map array between the two structures



        var fields = [
        {f: "auditoryNumber", p: "title"},
        {f: "floor", p: "floor"},
        {f: "corpus", p: "building"}
        ];

        this.fltRooms = this.rooms.filter(room =>
        fields.every(field => !formValues[field.f] || formValues[field.f] == room[field.p]);
        )






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 14:55









        James

        11.6k21430




        11.6k21430






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376611%2fhow-to-use-filter-with-conditionals-inside%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin