How do I retrieve a list of active items from an Array?










1















I'm trying to get a list of employees who only have active hobbies and whose role is of type 'A'.



I tried using the below query but without any success.



What am I doing wrong?






// Get all the employees with active hobbies and have a role of 'A'
var employees = people.find(item => item.key === 'Employees').employees.map(emp => emp.hobbies).filter(hobby => hobby.filter(h => h.active === true && h.roles.includes('A')));

console.log(employees);

<script>
var people = [
key: 'Employees',
employees: [
name: 'joe',
age: 20,
hobbies: [
'active': true,
name: 'skating',
roles: ['C', 'A']
]
,

name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']
]
,

name: 'kate',
age: 34,
hobbies: [
'active': true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']
]

]
];
</script>





Update



When I added more employees with new hobbies to the array, the accepted answer fails to produce the correct output. Why does this happen?



var people = [
key: 'Employees',
employees: [
name: 'Steve',
age: 50,
hobbies: [
active: true,
name: 'skating',
roles: ['C', 'A']
,

active: false,
name: 'skating',
roles: ['C', 'A']
,

active: true,
name: 'snooker',
roles: ['C', 'A']
,

active: true,
name: 'darts',
roles: ['C', 'A']

]
,
name: 'joe',
age: 20,
hobbies: [
active: true,
name: 'skating',
roles: ['C', 'A']

]
,
name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']

]
,
name: 'kate',
age: 34,
hobbies: [
active: true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']

]

]

];

var employees = people.find(item => item.key === 'Employees').employees.filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));









share|improve this question



















  • 1





    Just to clarify: "who only have active hobbies" means that all their hobbies have to be active? or just at least one? And "whose role is of type A" means they got at leadt one role? or their active hobby has to have that role? or all their hobbies have to have that role?

    – Jonas Wilms
    Nov 13 '18 at 19:56
















1















I'm trying to get a list of employees who only have active hobbies and whose role is of type 'A'.



I tried using the below query but without any success.



What am I doing wrong?






// Get all the employees with active hobbies and have a role of 'A'
var employees = people.find(item => item.key === 'Employees').employees.map(emp => emp.hobbies).filter(hobby => hobby.filter(h => h.active === true && h.roles.includes('A')));

console.log(employees);

<script>
var people = [
key: 'Employees',
employees: [
name: 'joe',
age: 20,
hobbies: [
'active': true,
name: 'skating',
roles: ['C', 'A']
]
,

name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']
]
,

name: 'kate',
age: 34,
hobbies: [
'active': true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']
]

]
];
</script>





Update



When I added more employees with new hobbies to the array, the accepted answer fails to produce the correct output. Why does this happen?



var people = [
key: 'Employees',
employees: [
name: 'Steve',
age: 50,
hobbies: [
active: true,
name: 'skating',
roles: ['C', 'A']
,

active: false,
name: 'skating',
roles: ['C', 'A']
,

active: true,
name: 'snooker',
roles: ['C', 'A']
,

active: true,
name: 'darts',
roles: ['C', 'A']

]
,
name: 'joe',
age: 20,
hobbies: [
active: true,
name: 'skating',
roles: ['C', 'A']

]
,
name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']

]
,
name: 'kate',
age: 34,
hobbies: [
active: true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']

]

]

];

var employees = people.find(item => item.key === 'Employees').employees.filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));









share|improve this question



















  • 1





    Just to clarify: "who only have active hobbies" means that all their hobbies have to be active? or just at least one? And "whose role is of type A" means they got at leadt one role? or their active hobby has to have that role? or all their hobbies have to have that role?

    – Jonas Wilms
    Nov 13 '18 at 19:56














1












1








1








I'm trying to get a list of employees who only have active hobbies and whose role is of type 'A'.



I tried using the below query but without any success.



What am I doing wrong?






// Get all the employees with active hobbies and have a role of 'A'
var employees = people.find(item => item.key === 'Employees').employees.map(emp => emp.hobbies).filter(hobby => hobby.filter(h => h.active === true && h.roles.includes('A')));

console.log(employees);

<script>
var people = [
key: 'Employees',
employees: [
name: 'joe',
age: 20,
hobbies: [
'active': true,
name: 'skating',
roles: ['C', 'A']
]
,

name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']
]
,

name: 'kate',
age: 34,
hobbies: [
'active': true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']
]

]
];
</script>





Update



When I added more employees with new hobbies to the array, the accepted answer fails to produce the correct output. Why does this happen?



var people = [
key: 'Employees',
employees: [
name: 'Steve',
age: 50,
hobbies: [
active: true,
name: 'skating',
roles: ['C', 'A']
,

active: false,
name: 'skating',
roles: ['C', 'A']
,

active: true,
name: 'snooker',
roles: ['C', 'A']
,

active: true,
name: 'darts',
roles: ['C', 'A']

]
,
name: 'joe',
age: 20,
hobbies: [
active: true,
name: 'skating',
roles: ['C', 'A']

]
,
name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']

]
,
name: 'kate',
age: 34,
hobbies: [
active: true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']

]

]

];

var employees = people.find(item => item.key === 'Employees').employees.filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));









share|improve this question
















I'm trying to get a list of employees who only have active hobbies and whose role is of type 'A'.



I tried using the below query but without any success.



What am I doing wrong?






// Get all the employees with active hobbies and have a role of 'A'
var employees = people.find(item => item.key === 'Employees').employees.map(emp => emp.hobbies).filter(hobby => hobby.filter(h => h.active === true && h.roles.includes('A')));

console.log(employees);

<script>
var people = [
key: 'Employees',
employees: [
name: 'joe',
age: 20,
hobbies: [
'active': true,
name: 'skating',
roles: ['C', 'A']
]
,

name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']
]
,

name: 'kate',
age: 34,
hobbies: [
'active': true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']
]

]
];
</script>





Update



When I added more employees with new hobbies to the array, the accepted answer fails to produce the correct output. Why does this happen?



var people = [
key: 'Employees',
employees: [
name: 'Steve',
age: 50,
hobbies: [
active: true,
name: 'skating',
roles: ['C', 'A']
,

active: false,
name: 'skating',
roles: ['C', 'A']
,

active: true,
name: 'snooker',
roles: ['C', 'A']
,

active: true,
name: 'darts',
roles: ['C', 'A']

]
,
name: 'joe',
age: 20,
hobbies: [
active: true,
name: 'skating',
roles: ['C', 'A']

]
,
name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']

]
,
name: 'kate',
age: 34,
hobbies: [
active: true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']

]

]

];

var employees = people.find(item => item.key === 'Employees').employees.filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));





// Get all the employees with active hobbies and have a role of 'A'
var employees = people.find(item => item.key === 'Employees').employees.map(emp => emp.hobbies).filter(hobby => hobby.filter(h => h.active === true && h.roles.includes('A')));

console.log(employees);

<script>
var people = [
key: 'Employees',
employees: [
name: 'joe',
age: 20,
hobbies: [
'active': true,
name: 'skating',
roles: ['C', 'A']
]
,

name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']
]
,

name: 'kate',
age: 34,
hobbies: [
'active': true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']
]

]
];
</script>





// Get all the employees with active hobbies and have a role of 'A'
var employees = people.find(item => item.key === 'Employees').employees.map(emp => emp.hobbies).filter(hobby => hobby.filter(h => h.active === true && h.roles.includes('A')));

console.log(employees);

<script>
var people = [
key: 'Employees',
employees: [
name: 'joe',
age: 20,
hobbies: [
'active': true,
name: 'skating',
roles: ['C', 'A']
]
,

name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']
]
,

name: 'kate',
age: 34,
hobbies: [
'active': true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']
]

]
];
</script>






javascript arrays dictionary filter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 21:14







QCar

















asked Nov 13 '18 at 19:44









QCarQCar

1691033




1691033







  • 1





    Just to clarify: "who only have active hobbies" means that all their hobbies have to be active? or just at least one? And "whose role is of type A" means they got at leadt one role? or their active hobby has to have that role? or all their hobbies have to have that role?

    – Jonas Wilms
    Nov 13 '18 at 19:56













  • 1





    Just to clarify: "who only have active hobbies" means that all their hobbies have to be active? or just at least one? And "whose role is of type A" means they got at leadt one role? or their active hobby has to have that role? or all their hobbies have to have that role?

    – Jonas Wilms
    Nov 13 '18 at 19:56








1




1





Just to clarify: "who only have active hobbies" means that all their hobbies have to be active? or just at least one? And "whose role is of type A" means they got at leadt one role? or their active hobby has to have that role? or all their hobbies have to have that role?

– Jonas Wilms
Nov 13 '18 at 19:56






Just to clarify: "who only have active hobbies" means that all their hobbies have to be active? or just at least one? And "whose role is of type A" means they got at leadt one role? or their active hobby has to have that role? or all their hobbies have to have that role?

– Jonas Wilms
Nov 13 '18 at 19:56













5 Answers
5






active

oldest

votes


















1














Things go wrong where you map the employees to their hobbies: this will make your final result consist of hobbies, not employees.



You need to stick to the employee level:






var people = [key: 'Employees',employees: [ name: 'joe', age: 20, hobbies: ['active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: ['active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: ['active': true, name: 'running', roles: ['C'], name: 'Chess', active: false, roles: ['C','A']] ]];

var employees = people.find(item => item.key === 'Employees').employees
.filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));

console.log(employees);





In an expression there is no need to compare a boolean property with true. Just use the property (active in this case).



Use .some instead of .every if the requirement is that employees have at least one such hobby, instead of requiring that all their hobbies comply with the condition.






share|improve this answer




















  • 1





    You've given a very detailed answer and have answered my question. Thanks

    – QCar
    Nov 13 '18 at 20:04











  • You're welcome ;-)

    – trincot
    Nov 13 '18 at 20:06











  • I just updated my question again, it seems that the code you submitted does not produce the desired result when I add more items to the array. Please see above edit. Thanks

    – QCar
    Nov 13 '18 at 21:04







  • 1





    For that data 3 employees are returned. Kate is not among them because not every hobby she has is marked active. I can only repeat the last paragraph of my answer, since your question left ambiguous which of the two logics you were looking for. See also the comment made to your question by Jonas which you did not answer. I believe my answer is correct. You just have to choose .every or .some depending on your expectations.

    – trincot
    Nov 13 '18 at 21:50


















1














.map(emp => emp.hobbies) returns an array of the hobbies, so the value of employees will be the filtered list of hobbies, not the employees that have those hobbies. You need to filter the employees, not map them.






// Get all the employees with active hobbies and have a role of 'A'
var employees = people.find(item => item.key === 'Employees').employees.filter(emp =>
emp.hobbies.every(h => h.active && h.roles.includes('A')));

console.log(employees);

<script>
var people = [
key: 'Employees',
employees: [
name: 'joe',
age: 20,
hobbies: [
'active': true,
name: 'skating',
roles: ['C', 'A']
]
,

name: 'amy',
age: 32,
hobbies: [
'active': true,
name: 'surfing',
roles: ['A']
]
,

name: 'kate',
age: 34,
hobbies: [
'active': true,
name: 'running',
roles: ['C']
,
name: 'Chess',
active: false,
roles: ['C', 'A']
]

]
];
</script>








share|improve this answer






























    1














    You can simply do one call to Array.prototype.filter which checks the conditions you mentioned:



    person.hobbies.every(y => y.active) && person.hobbies.every(z => z.roles.includes('A'))





    var people = [
    key: 'Employees',
    employees: [
    name: 'joe',
    age: 20,
    hobbies: [
    'active': true,
    name: 'skating',
    roles: ['C', 'A']
    ]
    ,

    name: 'amy',
    age: 32,
    hobbies: [
    'active': true,
    name: 'surfing',
    roles: ['A']
    ]
    ,

    name: 'kate',
    age: 34,
    hobbies: [
    'active': true,
    name: 'running',
    roles: ['C']
    ,
    name: 'Chess',
    active: false,
    roles: ['C', 'A']
    ]

    ]
    ];

    let employees = people[0].employees.filter(x =>
    x.hobbies.every(y => y.active) && x.hobbies.every(z => z.roles.includes('A'))
    )

    console.log(employees);








    share|improve this answer






























      1














      If you do this:



       .map(emp => emp.hobbies).filter(hobby =>


      you map every employee to its hobbies, which fill result in a 2D array:



       [[ active: true , active: false ], [/*...*/]]


      Therefore hobby is not one hobby but an array of hobbies.



      You said




      I'm trying to get a list of employees




      ... which means you actually don't want to .map to the hobbies, but rather .filter the employees and check if ever hobby fullfills certain rules:



       const employees = people.find(( key ) => key === "Employees").employees;

      const isActive = hobby => hobby.active && hobby.roles.includes("A");

      const result = employees.filter(emp => emp.hobbies.every(isActive));





      share|improve this answer

























      • this is exactly what i want but the query isn't working as expected. I only want to display a list of employees with Active hobbies and who has a role of 'A'. I tried running this query but it does not work : var result = people.find(item => item.key === 'Employees').employees.filter(emp => emp.hobbies.every(h => h.active && h.roles.includes('A')))

        – QCar
        Nov 13 '18 at 22:46


















      0














      You can use map & filter:






      var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

      const result = people.filter(x => x.key == 'Employees')
      .map((employees) =>
      employees.filter(x => x.hobbies.some(y => y.active && y.roles.includes('A'))))

      console.log(result)





      you could also use reduce & filter:






      var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

      const result = people.filter(x => x.key == 'Employees').reduce((r,employees) =>

      r.push(employees.filter(x =>
      x.hobbies.some(y => y.active && y.roles.includes('A'))))
      return r
      , )

      console.log(result)








      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',
        autoActivateHeartbeat: false,
        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%2f53288403%2fhow-do-i-retrieve-a-list-of-active-items-from-an-array%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









        1














        Things go wrong where you map the employees to their hobbies: this will make your final result consist of hobbies, not employees.



        You need to stick to the employee level:






        var people = [key: 'Employees',employees: [ name: 'joe', age: 20, hobbies: ['active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: ['active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: ['active': true, name: 'running', roles: ['C'], name: 'Chess', active: false, roles: ['C','A']] ]];

        var employees = people.find(item => item.key === 'Employees').employees
        .filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));

        console.log(employees);





        In an expression there is no need to compare a boolean property with true. Just use the property (active in this case).



        Use .some instead of .every if the requirement is that employees have at least one such hobby, instead of requiring that all their hobbies comply with the condition.






        share|improve this answer




















        • 1





          You've given a very detailed answer and have answered my question. Thanks

          – QCar
          Nov 13 '18 at 20:04











        • You're welcome ;-)

          – trincot
          Nov 13 '18 at 20:06











        • I just updated my question again, it seems that the code you submitted does not produce the desired result when I add more items to the array. Please see above edit. Thanks

          – QCar
          Nov 13 '18 at 21:04







        • 1





          For that data 3 employees are returned. Kate is not among them because not every hobby she has is marked active. I can only repeat the last paragraph of my answer, since your question left ambiguous which of the two logics you were looking for. See also the comment made to your question by Jonas which you did not answer. I believe my answer is correct. You just have to choose .every or .some depending on your expectations.

          – trincot
          Nov 13 '18 at 21:50















        1














        Things go wrong where you map the employees to their hobbies: this will make your final result consist of hobbies, not employees.



        You need to stick to the employee level:






        var people = [key: 'Employees',employees: [ name: 'joe', age: 20, hobbies: ['active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: ['active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: ['active': true, name: 'running', roles: ['C'], name: 'Chess', active: false, roles: ['C','A']] ]];

        var employees = people.find(item => item.key === 'Employees').employees
        .filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));

        console.log(employees);





        In an expression there is no need to compare a boolean property with true. Just use the property (active in this case).



        Use .some instead of .every if the requirement is that employees have at least one such hobby, instead of requiring that all their hobbies comply with the condition.






        share|improve this answer




















        • 1





          You've given a very detailed answer and have answered my question. Thanks

          – QCar
          Nov 13 '18 at 20:04











        • You're welcome ;-)

          – trincot
          Nov 13 '18 at 20:06











        • I just updated my question again, it seems that the code you submitted does not produce the desired result when I add more items to the array. Please see above edit. Thanks

          – QCar
          Nov 13 '18 at 21:04







        • 1





          For that data 3 employees are returned. Kate is not among them because not every hobby she has is marked active. I can only repeat the last paragraph of my answer, since your question left ambiguous which of the two logics you were looking for. See also the comment made to your question by Jonas which you did not answer. I believe my answer is correct. You just have to choose .every or .some depending on your expectations.

          – trincot
          Nov 13 '18 at 21:50













        1












        1








        1







        Things go wrong where you map the employees to their hobbies: this will make your final result consist of hobbies, not employees.



        You need to stick to the employee level:






        var people = [key: 'Employees',employees: [ name: 'joe', age: 20, hobbies: ['active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: ['active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: ['active': true, name: 'running', roles: ['C'], name: 'Chess', active: false, roles: ['C','A']] ]];

        var employees = people.find(item => item.key === 'Employees').employees
        .filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));

        console.log(employees);





        In an expression there is no need to compare a boolean property with true. Just use the property (active in this case).



        Use .some instead of .every if the requirement is that employees have at least one such hobby, instead of requiring that all their hobbies comply with the condition.






        share|improve this answer















        Things go wrong where you map the employees to their hobbies: this will make your final result consist of hobbies, not employees.



        You need to stick to the employee level:






        var people = [key: 'Employees',employees: [ name: 'joe', age: 20, hobbies: ['active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: ['active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: ['active': true, name: 'running', roles: ['C'], name: 'Chess', active: false, roles: ['C','A']] ]];

        var employees = people.find(item => item.key === 'Employees').employees
        .filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));

        console.log(employees);





        In an expression there is no need to compare a boolean property with true. Just use the property (active in this case).



        Use .some instead of .every if the requirement is that employees have at least one such hobby, instead of requiring that all their hobbies comply with the condition.






        var people = [key: 'Employees',employees: [ name: 'joe', age: 20, hobbies: ['active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: ['active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: ['active': true, name: 'running', roles: ['C'], name: 'Chess', active: false, roles: ['C','A']] ]];

        var employees = people.find(item => item.key === 'Employees').employees
        .filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));

        console.log(employees);





        var people = [key: 'Employees',employees: [ name: 'joe', age: 20, hobbies: ['active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: ['active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: ['active': true, name: 'running', roles: ['C'], name: 'Chess', active: false, roles: ['C','A']] ]];

        var employees = people.find(item => item.key === 'Employees').employees
        .filter(employee => employee.hobbies.every(h => h.active && h.roles.includes('A')));

        console.log(employees);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 20:06

























        answered Nov 13 '18 at 19:54









        trincottrincot

        121k1586118




        121k1586118







        • 1





          You've given a very detailed answer and have answered my question. Thanks

          – QCar
          Nov 13 '18 at 20:04











        • You're welcome ;-)

          – trincot
          Nov 13 '18 at 20:06











        • I just updated my question again, it seems that the code you submitted does not produce the desired result when I add more items to the array. Please see above edit. Thanks

          – QCar
          Nov 13 '18 at 21:04







        • 1





          For that data 3 employees are returned. Kate is not among them because not every hobby she has is marked active. I can only repeat the last paragraph of my answer, since your question left ambiguous which of the two logics you were looking for. See also the comment made to your question by Jonas which you did not answer. I believe my answer is correct. You just have to choose .every or .some depending on your expectations.

          – trincot
          Nov 13 '18 at 21:50












        • 1





          You've given a very detailed answer and have answered my question. Thanks

          – QCar
          Nov 13 '18 at 20:04











        • You're welcome ;-)

          – trincot
          Nov 13 '18 at 20:06











        • I just updated my question again, it seems that the code you submitted does not produce the desired result when I add more items to the array. Please see above edit. Thanks

          – QCar
          Nov 13 '18 at 21:04







        • 1





          For that data 3 employees are returned. Kate is not among them because not every hobby she has is marked active. I can only repeat the last paragraph of my answer, since your question left ambiguous which of the two logics you were looking for. See also the comment made to your question by Jonas which you did not answer. I believe my answer is correct. You just have to choose .every or .some depending on your expectations.

          – trincot
          Nov 13 '18 at 21:50







        1




        1





        You've given a very detailed answer and have answered my question. Thanks

        – QCar
        Nov 13 '18 at 20:04





        You've given a very detailed answer and have answered my question. Thanks

        – QCar
        Nov 13 '18 at 20:04













        You're welcome ;-)

        – trincot
        Nov 13 '18 at 20:06





        You're welcome ;-)

        – trincot
        Nov 13 '18 at 20:06













        I just updated my question again, it seems that the code you submitted does not produce the desired result when I add more items to the array. Please see above edit. Thanks

        – QCar
        Nov 13 '18 at 21:04






        I just updated my question again, it seems that the code you submitted does not produce the desired result when I add more items to the array. Please see above edit. Thanks

        – QCar
        Nov 13 '18 at 21:04





        1




        1





        For that data 3 employees are returned. Kate is not among them because not every hobby she has is marked active. I can only repeat the last paragraph of my answer, since your question left ambiguous which of the two logics you were looking for. See also the comment made to your question by Jonas which you did not answer. I believe my answer is correct. You just have to choose .every or .some depending on your expectations.

        – trincot
        Nov 13 '18 at 21:50





        For that data 3 employees are returned. Kate is not among them because not every hobby she has is marked active. I can only repeat the last paragraph of my answer, since your question left ambiguous which of the two logics you were looking for. See also the comment made to your question by Jonas which you did not answer. I believe my answer is correct. You just have to choose .every or .some depending on your expectations.

        – trincot
        Nov 13 '18 at 21:50













        1














        .map(emp => emp.hobbies) returns an array of the hobbies, so the value of employees will be the filtered list of hobbies, not the employees that have those hobbies. You need to filter the employees, not map them.






        // Get all the employees with active hobbies and have a role of 'A'
        var employees = people.find(item => item.key === 'Employees').employees.filter(emp =>
        emp.hobbies.every(h => h.active && h.roles.includes('A')));

        console.log(employees);

        <script>
        var people = [
        key: 'Employees',
        employees: [
        name: 'joe',
        age: 20,
        hobbies: [
        'active': true,
        name: 'skating',
        roles: ['C', 'A']
        ]
        ,

        name: 'amy',
        age: 32,
        hobbies: [
        'active': true,
        name: 'surfing',
        roles: ['A']
        ]
        ,

        name: 'kate',
        age: 34,
        hobbies: [
        'active': true,
        name: 'running',
        roles: ['C']
        ,
        name: 'Chess',
        active: false,
        roles: ['C', 'A']
        ]

        ]
        ];
        </script>








        share|improve this answer



























          1














          .map(emp => emp.hobbies) returns an array of the hobbies, so the value of employees will be the filtered list of hobbies, not the employees that have those hobbies. You need to filter the employees, not map them.






          // Get all the employees with active hobbies and have a role of 'A'
          var employees = people.find(item => item.key === 'Employees').employees.filter(emp =>
          emp.hobbies.every(h => h.active && h.roles.includes('A')));

          console.log(employees);

          <script>
          var people = [
          key: 'Employees',
          employees: [
          name: 'joe',
          age: 20,
          hobbies: [
          'active': true,
          name: 'skating',
          roles: ['C', 'A']
          ]
          ,

          name: 'amy',
          age: 32,
          hobbies: [
          'active': true,
          name: 'surfing',
          roles: ['A']
          ]
          ,

          name: 'kate',
          age: 34,
          hobbies: [
          'active': true,
          name: 'running',
          roles: ['C']
          ,
          name: 'Chess',
          active: false,
          roles: ['C', 'A']
          ]

          ]
          ];
          </script>








          share|improve this answer

























            1












            1








            1







            .map(emp => emp.hobbies) returns an array of the hobbies, so the value of employees will be the filtered list of hobbies, not the employees that have those hobbies. You need to filter the employees, not map them.






            // Get all the employees with active hobbies and have a role of 'A'
            var employees = people.find(item => item.key === 'Employees').employees.filter(emp =>
            emp.hobbies.every(h => h.active && h.roles.includes('A')));

            console.log(employees);

            <script>
            var people = [
            key: 'Employees',
            employees: [
            name: 'joe',
            age: 20,
            hobbies: [
            'active': true,
            name: 'skating',
            roles: ['C', 'A']
            ]
            ,

            name: 'amy',
            age: 32,
            hobbies: [
            'active': true,
            name: 'surfing',
            roles: ['A']
            ]
            ,

            name: 'kate',
            age: 34,
            hobbies: [
            'active': true,
            name: 'running',
            roles: ['C']
            ,
            name: 'Chess',
            active: false,
            roles: ['C', 'A']
            ]

            ]
            ];
            </script>








            share|improve this answer













            .map(emp => emp.hobbies) returns an array of the hobbies, so the value of employees will be the filtered list of hobbies, not the employees that have those hobbies. You need to filter the employees, not map them.






            // Get all the employees with active hobbies and have a role of 'A'
            var employees = people.find(item => item.key === 'Employees').employees.filter(emp =>
            emp.hobbies.every(h => h.active && h.roles.includes('A')));

            console.log(employees);

            <script>
            var people = [
            key: 'Employees',
            employees: [
            name: 'joe',
            age: 20,
            hobbies: [
            'active': true,
            name: 'skating',
            roles: ['C', 'A']
            ]
            ,

            name: 'amy',
            age: 32,
            hobbies: [
            'active': true,
            name: 'surfing',
            roles: ['A']
            ]
            ,

            name: 'kate',
            age: 34,
            hobbies: [
            'active': true,
            name: 'running',
            roles: ['C']
            ,
            name: 'Chess',
            active: false,
            roles: ['C', 'A']
            ]

            ]
            ];
            </script>








            // Get all the employees with active hobbies and have a role of 'A'
            var employees = people.find(item => item.key === 'Employees').employees.filter(emp =>
            emp.hobbies.every(h => h.active && h.roles.includes('A')));

            console.log(employees);

            <script>
            var people = [
            key: 'Employees',
            employees: [
            name: 'joe',
            age: 20,
            hobbies: [
            'active': true,
            name: 'skating',
            roles: ['C', 'A']
            ]
            ,

            name: 'amy',
            age: 32,
            hobbies: [
            'active': true,
            name: 'surfing',
            roles: ['A']
            ]
            ,

            name: 'kate',
            age: 34,
            hobbies: [
            'active': true,
            name: 'running',
            roles: ['C']
            ,
            name: 'Chess',
            active: false,
            roles: ['C', 'A']
            ]

            ]
            ];
            </script>





            // Get all the employees with active hobbies and have a role of 'A'
            var employees = people.find(item => item.key === 'Employees').employees.filter(emp =>
            emp.hobbies.every(h => h.active && h.roles.includes('A')));

            console.log(employees);

            <script>
            var people = [
            key: 'Employees',
            employees: [
            name: 'joe',
            age: 20,
            hobbies: [
            'active': true,
            name: 'skating',
            roles: ['C', 'A']
            ]
            ,

            name: 'amy',
            age: 32,
            hobbies: [
            'active': true,
            name: 'surfing',
            roles: ['A']
            ]
            ,

            name: 'kate',
            age: 34,
            hobbies: [
            'active': true,
            name: 'running',
            roles: ['C']
            ,
            name: 'Chess',
            active: false,
            roles: ['C', 'A']
            ]

            ]
            ];
            </script>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 19:54









            BarmarBarmar

            424k35248349




            424k35248349





















                1














                You can simply do one call to Array.prototype.filter which checks the conditions you mentioned:



                person.hobbies.every(y => y.active) && person.hobbies.every(z => z.roles.includes('A'))





                var people = [
                key: 'Employees',
                employees: [
                name: 'joe',
                age: 20,
                hobbies: [
                'active': true,
                name: 'skating',
                roles: ['C', 'A']
                ]
                ,

                name: 'amy',
                age: 32,
                hobbies: [
                'active': true,
                name: 'surfing',
                roles: ['A']
                ]
                ,

                name: 'kate',
                age: 34,
                hobbies: [
                'active': true,
                name: 'running',
                roles: ['C']
                ,
                name: 'Chess',
                active: false,
                roles: ['C', 'A']
                ]

                ]
                ];

                let employees = people[0].employees.filter(x =>
                x.hobbies.every(y => y.active) && x.hobbies.every(z => z.roles.includes('A'))
                )

                console.log(employees);








                share|improve this answer



























                  1














                  You can simply do one call to Array.prototype.filter which checks the conditions you mentioned:



                  person.hobbies.every(y => y.active) && person.hobbies.every(z => z.roles.includes('A'))





                  var people = [
                  key: 'Employees',
                  employees: [
                  name: 'joe',
                  age: 20,
                  hobbies: [
                  'active': true,
                  name: 'skating',
                  roles: ['C', 'A']
                  ]
                  ,

                  name: 'amy',
                  age: 32,
                  hobbies: [
                  'active': true,
                  name: 'surfing',
                  roles: ['A']
                  ]
                  ,

                  name: 'kate',
                  age: 34,
                  hobbies: [
                  'active': true,
                  name: 'running',
                  roles: ['C']
                  ,
                  name: 'Chess',
                  active: false,
                  roles: ['C', 'A']
                  ]

                  ]
                  ];

                  let employees = people[0].employees.filter(x =>
                  x.hobbies.every(y => y.active) && x.hobbies.every(z => z.roles.includes('A'))
                  )

                  console.log(employees);








                  share|improve this answer

























                    1












                    1








                    1







                    You can simply do one call to Array.prototype.filter which checks the conditions you mentioned:



                    person.hobbies.every(y => y.active) && person.hobbies.every(z => z.roles.includes('A'))





                    var people = [
                    key: 'Employees',
                    employees: [
                    name: 'joe',
                    age: 20,
                    hobbies: [
                    'active': true,
                    name: 'skating',
                    roles: ['C', 'A']
                    ]
                    ,

                    name: 'amy',
                    age: 32,
                    hobbies: [
                    'active': true,
                    name: 'surfing',
                    roles: ['A']
                    ]
                    ,

                    name: 'kate',
                    age: 34,
                    hobbies: [
                    'active': true,
                    name: 'running',
                    roles: ['C']
                    ,
                    name: 'Chess',
                    active: false,
                    roles: ['C', 'A']
                    ]

                    ]
                    ];

                    let employees = people[0].employees.filter(x =>
                    x.hobbies.every(y => y.active) && x.hobbies.every(z => z.roles.includes('A'))
                    )

                    console.log(employees);








                    share|improve this answer













                    You can simply do one call to Array.prototype.filter which checks the conditions you mentioned:



                    person.hobbies.every(y => y.active) && person.hobbies.every(z => z.roles.includes('A'))





                    var people = [
                    key: 'Employees',
                    employees: [
                    name: 'joe',
                    age: 20,
                    hobbies: [
                    'active': true,
                    name: 'skating',
                    roles: ['C', 'A']
                    ]
                    ,

                    name: 'amy',
                    age: 32,
                    hobbies: [
                    'active': true,
                    name: 'surfing',
                    roles: ['A']
                    ]
                    ,

                    name: 'kate',
                    age: 34,
                    hobbies: [
                    'active': true,
                    name: 'running',
                    roles: ['C']
                    ,
                    name: 'Chess',
                    active: false,
                    roles: ['C', 'A']
                    ]

                    ]
                    ];

                    let employees = people[0].employees.filter(x =>
                    x.hobbies.every(y => y.active) && x.hobbies.every(z => z.roles.includes('A'))
                    )

                    console.log(employees);








                    var people = [
                    key: 'Employees',
                    employees: [
                    name: 'joe',
                    age: 20,
                    hobbies: [
                    'active': true,
                    name: 'skating',
                    roles: ['C', 'A']
                    ]
                    ,

                    name: 'amy',
                    age: 32,
                    hobbies: [
                    'active': true,
                    name: 'surfing',
                    roles: ['A']
                    ]
                    ,

                    name: 'kate',
                    age: 34,
                    hobbies: [
                    'active': true,
                    name: 'running',
                    roles: ['C']
                    ,
                    name: 'Chess',
                    active: false,
                    roles: ['C', 'A']
                    ]

                    ]
                    ];

                    let employees = people[0].employees.filter(x =>
                    x.hobbies.every(y => y.active) && x.hobbies.every(z => z.roles.includes('A'))
                    )

                    console.log(employees);





                    var people = [
                    key: 'Employees',
                    employees: [
                    name: 'joe',
                    age: 20,
                    hobbies: [
                    'active': true,
                    name: 'skating',
                    roles: ['C', 'A']
                    ]
                    ,

                    name: 'amy',
                    age: 32,
                    hobbies: [
                    'active': true,
                    name: 'surfing',
                    roles: ['A']
                    ]
                    ,

                    name: 'kate',
                    age: 34,
                    hobbies: [
                    'active': true,
                    name: 'running',
                    roles: ['C']
                    ,
                    name: 'Chess',
                    active: false,
                    roles: ['C', 'A']
                    ]

                    ]
                    ];

                    let employees = people[0].employees.filter(x =>
                    x.hobbies.every(y => y.active) && x.hobbies.every(z => z.roles.includes('A'))
                    )

                    console.log(employees);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 19:56









                    connexoconnexo

                    21.6k73556




                    21.6k73556





















                        1














                        If you do this:



                         .map(emp => emp.hobbies).filter(hobby =>


                        you map every employee to its hobbies, which fill result in a 2D array:



                         [[ active: true , active: false ], [/*...*/]]


                        Therefore hobby is not one hobby but an array of hobbies.



                        You said




                        I'm trying to get a list of employees




                        ... which means you actually don't want to .map to the hobbies, but rather .filter the employees and check if ever hobby fullfills certain rules:



                         const employees = people.find(( key ) => key === "Employees").employees;

                        const isActive = hobby => hobby.active && hobby.roles.includes("A");

                        const result = employees.filter(emp => emp.hobbies.every(isActive));





                        share|improve this answer

























                        • this is exactly what i want but the query isn't working as expected. I only want to display a list of employees with Active hobbies and who has a role of 'A'. I tried running this query but it does not work : var result = people.find(item => item.key === 'Employees').employees.filter(emp => emp.hobbies.every(h => h.active && h.roles.includes('A')))

                          – QCar
                          Nov 13 '18 at 22:46















                        1














                        If you do this:



                         .map(emp => emp.hobbies).filter(hobby =>


                        you map every employee to its hobbies, which fill result in a 2D array:



                         [[ active: true , active: false ], [/*...*/]]


                        Therefore hobby is not one hobby but an array of hobbies.



                        You said




                        I'm trying to get a list of employees




                        ... which means you actually don't want to .map to the hobbies, but rather .filter the employees and check if ever hobby fullfills certain rules:



                         const employees = people.find(( key ) => key === "Employees").employees;

                        const isActive = hobby => hobby.active && hobby.roles.includes("A");

                        const result = employees.filter(emp => emp.hobbies.every(isActive));





                        share|improve this answer

























                        • this is exactly what i want but the query isn't working as expected. I only want to display a list of employees with Active hobbies and who has a role of 'A'. I tried running this query but it does not work : var result = people.find(item => item.key === 'Employees').employees.filter(emp => emp.hobbies.every(h => h.active && h.roles.includes('A')))

                          – QCar
                          Nov 13 '18 at 22:46













                        1












                        1








                        1







                        If you do this:



                         .map(emp => emp.hobbies).filter(hobby =>


                        you map every employee to its hobbies, which fill result in a 2D array:



                         [[ active: true , active: false ], [/*...*/]]


                        Therefore hobby is not one hobby but an array of hobbies.



                        You said




                        I'm trying to get a list of employees




                        ... which means you actually don't want to .map to the hobbies, but rather .filter the employees and check if ever hobby fullfills certain rules:



                         const employees = people.find(( key ) => key === "Employees").employees;

                        const isActive = hobby => hobby.active && hobby.roles.includes("A");

                        const result = employees.filter(emp => emp.hobbies.every(isActive));





                        share|improve this answer















                        If you do this:



                         .map(emp => emp.hobbies).filter(hobby =>


                        you map every employee to its hobbies, which fill result in a 2D array:



                         [[ active: true , active: false ], [/*...*/]]


                        Therefore hobby is not one hobby but an array of hobbies.



                        You said




                        I'm trying to get a list of employees




                        ... which means you actually don't want to .map to the hobbies, but rather .filter the employees and check if ever hobby fullfills certain rules:



                         const employees = people.find(( key ) => key === "Employees").employees;

                        const isActive = hobby => hobby.active && hobby.roles.includes("A");

                        const result = employees.filter(emp => emp.hobbies.every(isActive));






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 13 '18 at 19:58

























                        answered Nov 13 '18 at 19:52









                        Jonas WilmsJonas Wilms

                        56.9k42851




                        56.9k42851












                        • this is exactly what i want but the query isn't working as expected. I only want to display a list of employees with Active hobbies and who has a role of 'A'. I tried running this query but it does not work : var result = people.find(item => item.key === 'Employees').employees.filter(emp => emp.hobbies.every(h => h.active && h.roles.includes('A')))

                          – QCar
                          Nov 13 '18 at 22:46

















                        • this is exactly what i want but the query isn't working as expected. I only want to display a list of employees with Active hobbies and who has a role of 'A'. I tried running this query but it does not work : var result = people.find(item => item.key === 'Employees').employees.filter(emp => emp.hobbies.every(h => h.active && h.roles.includes('A')))

                          – QCar
                          Nov 13 '18 at 22:46
















                        this is exactly what i want but the query isn't working as expected. I only want to display a list of employees with Active hobbies and who has a role of 'A'. I tried running this query but it does not work : var result = people.find(item => item.key === 'Employees').employees.filter(emp => emp.hobbies.every(h => h.active && h.roles.includes('A')))

                        – QCar
                        Nov 13 '18 at 22:46





                        this is exactly what i want but the query isn't working as expected. I only want to display a list of employees with Active hobbies and who has a role of 'A'. I tried running this query but it does not work : var result = people.find(item => item.key === 'Employees').employees.filter(emp => emp.hobbies.every(h => h.active && h.roles.includes('A')))

                        – QCar
                        Nov 13 '18 at 22:46











                        0














                        You can use map & filter:






                        var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                        const result = people.filter(x => x.key == 'Employees')
                        .map((employees) =>
                        employees.filter(x => x.hobbies.some(y => y.active && y.roles.includes('A'))))

                        console.log(result)





                        you could also use reduce & filter:






                        var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                        const result = people.filter(x => x.key == 'Employees').reduce((r,employees) =>

                        r.push(employees.filter(x =>
                        x.hobbies.some(y => y.active && y.roles.includes('A'))))
                        return r
                        , )

                        console.log(result)








                        share|improve this answer





























                          0














                          You can use map & filter:






                          var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                          const result = people.filter(x => x.key == 'Employees')
                          .map((employees) =>
                          employees.filter(x => x.hobbies.some(y => y.active && y.roles.includes('A'))))

                          console.log(result)





                          you could also use reduce & filter:






                          var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                          const result = people.filter(x => x.key == 'Employees').reduce((r,employees) =>

                          r.push(employees.filter(x =>
                          x.hobbies.some(y => y.active && y.roles.includes('A'))))
                          return r
                          , )

                          console.log(result)








                          share|improve this answer



























                            0












                            0








                            0







                            You can use map & filter:






                            var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                            const result = people.filter(x => x.key == 'Employees')
                            .map((employees) =>
                            employees.filter(x => x.hobbies.some(y => y.active && y.roles.includes('A'))))

                            console.log(result)





                            you could also use reduce & filter:






                            var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                            const result = people.filter(x => x.key == 'Employees').reduce((r,employees) =>

                            r.push(employees.filter(x =>
                            x.hobbies.some(y => y.active && y.roles.includes('A'))))
                            return r
                            , )

                            console.log(result)








                            share|improve this answer















                            You can use map & filter:






                            var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                            const result = people.filter(x => x.key == 'Employees')
                            .map((employees) =>
                            employees.filter(x => x.hobbies.some(y => y.active && y.roles.includes('A'))))

                            console.log(result)





                            you could also use reduce & filter:






                            var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                            const result = people.filter(x => x.key == 'Employees').reduce((r,employees) =>

                            r.push(employees.filter(x =>
                            x.hobbies.some(y => y.active && y.roles.includes('A'))))
                            return r
                            , )

                            console.log(result)








                            var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                            const result = people.filter(x => x.key == 'Employees')
                            .map((employees) =>
                            employees.filter(x => x.hobbies.some(y => y.active && y.roles.includes('A'))))

                            console.log(result)





                            var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                            const result = people.filter(x => x.key == 'Employees')
                            .map((employees) =>
                            employees.filter(x => x.hobbies.some(y => y.active && y.roles.includes('A'))))

                            console.log(result)





                            var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                            const result = people.filter(x => x.key == 'Employees').reduce((r,employees) =>

                            r.push(employees.filter(x =>
                            x.hobbies.some(y => y.active && y.roles.includes('A'))))
                            return r
                            , )

                            console.log(result)





                            var people = [ key: 'Employees', employees: [ name: 'joe', age: 20, hobbies: [ 'active': true, name: 'skating', roles: ['C', 'A'] ] , name: 'amy', age: 32, hobbies: [ 'active': true, name: 'surfing', roles: ['A'] ] , name: 'kate', age: 34, hobbies: [ 'active': true, name: 'running', roles: ['C'] , name: 'Chess', active: false, roles: ['C', 'A'] ] ] ];

                            const result = people.filter(x => x.key == 'Employees').reduce((r,employees) =>

                            r.push(employees.filter(x =>
                            x.hobbies.some(y => y.active && y.roles.includes('A'))))
                            return r
                            , )

                            console.log(result)






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 13 '18 at 20:03

























                            answered Nov 13 '18 at 19:54









                            AkrionAkrion

                            9,41711224




                            9,41711224



























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288403%2fhow-do-i-retrieve-a-list-of-active-items-from-an-array%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







                                這個網誌中的熱門文章

                                What does pagestruct do in Eviews?

                                Dutch intervention in Lombok and Karangasem

                                Channel Islands