How to get an array of unique values of a specific key provided I only have an array of id's of that object using ES6










0














I have a store set up for a list of candidate objects like this



const candidatesStore = [
id: 1, status: active
id:2, status: inactive
id: 3 status: failed
id: 4 status:inactive
id: 5 status:failed

.
... and so on ]


I only have the list of id's in an array, ex: requiredArray = [2,3,4]



How to get a list of unique statuses by just using requiredArray using ES6 in the most efficient way



The result should be [inactive, failed]
I preferably want to use reduce to get unique value, and find to get the list of candidates from candidatesStore










share|improve this question























  • What's going on with these candidate1 = assignments in your array literal?
    – Bergi
    Nov 12 '18 at 19:59










  • How about Array.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))? I don't see how reduce or find could be useful here.
    – Bergi
    Nov 12 '18 at 20:00
















0














I have a store set up for a list of candidate objects like this



const candidatesStore = [
id: 1, status: active
id:2, status: inactive
id: 3 status: failed
id: 4 status:inactive
id: 5 status:failed

.
... and so on ]


I only have the list of id's in an array, ex: requiredArray = [2,3,4]



How to get a list of unique statuses by just using requiredArray using ES6 in the most efficient way



The result should be [inactive, failed]
I preferably want to use reduce to get unique value, and find to get the list of candidates from candidatesStore










share|improve this question























  • What's going on with these candidate1 = assignments in your array literal?
    – Bergi
    Nov 12 '18 at 19:59










  • How about Array.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))? I don't see how reduce or find could be useful here.
    – Bergi
    Nov 12 '18 at 20:00














0












0








0







I have a store set up for a list of candidate objects like this



const candidatesStore = [
id: 1, status: active
id:2, status: inactive
id: 3 status: failed
id: 4 status:inactive
id: 5 status:failed

.
... and so on ]


I only have the list of id's in an array, ex: requiredArray = [2,3,4]



How to get a list of unique statuses by just using requiredArray using ES6 in the most efficient way



The result should be [inactive, failed]
I preferably want to use reduce to get unique value, and find to get the list of candidates from candidatesStore










share|improve this question















I have a store set up for a list of candidate objects like this



const candidatesStore = [
id: 1, status: active
id:2, status: inactive
id: 3 status: failed
id: 4 status:inactive
id: 5 status:failed

.
... and so on ]


I only have the list of id's in an array, ex: requiredArray = [2,3,4]



How to get a list of unique statuses by just using requiredArray using ES6 in the most efficient way



The result should be [inactive, failed]
I preferably want to use reduce to get unique value, and find to get the list of candidates from candidatesStore







javascript ecmascript-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 22:23

























asked Nov 12 '18 at 19:53









Anand Dharne

62




62











  • What's going on with these candidate1 = assignments in your array literal?
    – Bergi
    Nov 12 '18 at 19:59










  • How about Array.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))? I don't see how reduce or find could be useful here.
    – Bergi
    Nov 12 '18 at 20:00

















  • What's going on with these candidate1 = assignments in your array literal?
    – Bergi
    Nov 12 '18 at 19:59










  • How about Array.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))? I don't see how reduce or find could be useful here.
    – Bergi
    Nov 12 '18 at 20:00
















What's going on with these candidate1 = assignments in your array literal?
– Bergi
Nov 12 '18 at 19:59




What's going on with these candidate1 = assignments in your array literal?
– Bergi
Nov 12 '18 at 19:59












How about Array.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))? I don't see how reduce or find could be useful here.
– Bergi
Nov 12 '18 at 20:00





How about Array.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))? I don't see how reduce or find could be useful here.
– Bergi
Nov 12 '18 at 20:00













2 Answers
2






active

oldest

votes


















1














You can use "reduce" as well as "filter" for this.



Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.






const candidates = [
id: 1, status: 'active'
,id: 2, status: 'inactive'
,id: 3, status: 'failed'
,id: 4, status: 'inactive'
,id: 5, status: 'failed'
]

const requiredArray = [2,3,4]

let result = [...candidates.filter(( id ) => requiredArray.includes(id))
.reduce((s, d) => s.add(d.status) , new Set)
]

console.log(result)








share|improve this answer






















  • FWIW, you can also skip the .map step and do .reduce((s, d) => s.add(d.status) , new Set)
    – Felix Kling
    Nov 13 '18 at 1:44










  • Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
    – Nitish Narang
    Nov 13 '18 at 3:57






  • 1




    I'm glad to hear that and you are very welcome :)
    – Felix Kling
    Nov 13 '18 at 17:13


















0














Try this.






const candidates = [
id: 1, status: "active",
id: 2, status: "inactive",
id: 3, status: "failed",
id: 4, status: "inactive",
id: 5, status: "failed",
];
let requiredArray = [2,3,4];
let newArray = candidates.filter(x=>requiredArray.includes(x.id))
console.log(newArray);








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%2f53269178%2fhow-to-get-an-array-of-unique-values-of-a-specific-key-provided-i-only-have-an-a%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can use "reduce" as well as "filter" for this.



    Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.






    const candidates = [
    id: 1, status: 'active'
    ,id: 2, status: 'inactive'
    ,id: 3, status: 'failed'
    ,id: 4, status: 'inactive'
    ,id: 5, status: 'failed'
    ]

    const requiredArray = [2,3,4]

    let result = [...candidates.filter(( id ) => requiredArray.includes(id))
    .reduce((s, d) => s.add(d.status) , new Set)
    ]

    console.log(result)








    share|improve this answer






















    • FWIW, you can also skip the .map step and do .reduce((s, d) => s.add(d.status) , new Set)
      – Felix Kling
      Nov 13 '18 at 1:44










    • Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
      – Nitish Narang
      Nov 13 '18 at 3:57






    • 1




      I'm glad to hear that and you are very welcome :)
      – Felix Kling
      Nov 13 '18 at 17:13















    1














    You can use "reduce" as well as "filter" for this.



    Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.






    const candidates = [
    id: 1, status: 'active'
    ,id: 2, status: 'inactive'
    ,id: 3, status: 'failed'
    ,id: 4, status: 'inactive'
    ,id: 5, status: 'failed'
    ]

    const requiredArray = [2,3,4]

    let result = [...candidates.filter(( id ) => requiredArray.includes(id))
    .reduce((s, d) => s.add(d.status) , new Set)
    ]

    console.log(result)








    share|improve this answer






















    • FWIW, you can also skip the .map step and do .reduce((s, d) => s.add(d.status) , new Set)
      – Felix Kling
      Nov 13 '18 at 1:44










    • Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
      – Nitish Narang
      Nov 13 '18 at 3:57






    • 1




      I'm glad to hear that and you are very welcome :)
      – Felix Kling
      Nov 13 '18 at 17:13













    1












    1








    1






    You can use "reduce" as well as "filter" for this.



    Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.






    const candidates = [
    id: 1, status: 'active'
    ,id: 2, status: 'inactive'
    ,id: 3, status: 'failed'
    ,id: 4, status: 'inactive'
    ,id: 5, status: 'failed'
    ]

    const requiredArray = [2,3,4]

    let result = [...candidates.filter(( id ) => requiredArray.includes(id))
    .reduce((s, d) => s.add(d.status) , new Set)
    ]

    console.log(result)








    share|improve this answer














    You can use "reduce" as well as "filter" for this.



    Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.






    const candidates = [
    id: 1, status: 'active'
    ,id: 2, status: 'inactive'
    ,id: 3, status: 'failed'
    ,id: 4, status: 'inactive'
    ,id: 5, status: 'failed'
    ]

    const requiredArray = [2,3,4]

    let result = [...candidates.filter(( id ) => requiredArray.includes(id))
    .reduce((s, d) => s.add(d.status) , new Set)
    ]

    console.log(result)








    const candidates = [
    id: 1, status: 'active'
    ,id: 2, status: 'inactive'
    ,id: 3, status: 'failed'
    ,id: 4, status: 'inactive'
    ,id: 5, status: 'failed'
    ]

    const requiredArray = [2,3,4]

    let result = [...candidates.filter(( id ) => requiredArray.includes(id))
    .reduce((s, d) => s.add(d.status) , new Set)
    ]

    console.log(result)





    const candidates = [
    id: 1, status: 'active'
    ,id: 2, status: 'inactive'
    ,id: 3, status: 'failed'
    ,id: 4, status: 'inactive'
    ,id: 5, status: 'failed'
    ]

    const requiredArray = [2,3,4]

    let result = [...candidates.filter(( id ) => requiredArray.includes(id))
    .reduce((s, d) => s.add(d.status) , new Set)
    ]

    console.log(result)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 6:38

























    answered Nov 12 '18 at 20:01









    Nitish Narang

    2,938815




    2,938815











    • FWIW, you can also skip the .map step and do .reduce((s, d) => s.add(d.status) , new Set)
      – Felix Kling
      Nov 13 '18 at 1:44










    • Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
      – Nitish Narang
      Nov 13 '18 at 3:57






    • 1




      I'm glad to hear that and you are very welcome :)
      – Felix Kling
      Nov 13 '18 at 17:13
















    • FWIW, you can also skip the .map step and do .reduce((s, d) => s.add(d.status) , new Set)
      – Felix Kling
      Nov 13 '18 at 1:44










    • Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
      – Nitish Narang
      Nov 13 '18 at 3:57






    • 1




      I'm glad to hear that and you are very welcome :)
      – Felix Kling
      Nov 13 '18 at 17:13















    FWIW, you can also skip the .map step and do .reduce((s, d) => s.add(d.status) , new Set)
    – Felix Kling
    Nov 13 '18 at 1:44




    FWIW, you can also skip the .map step and do .reduce((s, d) => s.add(d.status) , new Set)
    – Felix Kling
    Nov 13 '18 at 1:44












    Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
    – Nitish Narang
    Nov 13 '18 at 3:57




    Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
    – Nitish Narang
    Nov 13 '18 at 3:57




    1




    1




    I'm glad to hear that and you are very welcome :)
    – Felix Kling
    Nov 13 '18 at 17:13




    I'm glad to hear that and you are very welcome :)
    – Felix Kling
    Nov 13 '18 at 17:13













    0














    Try this.






    const candidates = [
    id: 1, status: "active",
    id: 2, status: "inactive",
    id: 3, status: "failed",
    id: 4, status: "inactive",
    id: 5, status: "failed",
    ];
    let requiredArray = [2,3,4];
    let newArray = candidates.filter(x=>requiredArray.includes(x.id))
    console.log(newArray);








    share|improve this answer

























      0














      Try this.






      const candidates = [
      id: 1, status: "active",
      id: 2, status: "inactive",
      id: 3, status: "failed",
      id: 4, status: "inactive",
      id: 5, status: "failed",
      ];
      let requiredArray = [2,3,4];
      let newArray = candidates.filter(x=>requiredArray.includes(x.id))
      console.log(newArray);








      share|improve this answer























        0












        0








        0






        Try this.






        const candidates = [
        id: 1, status: "active",
        id: 2, status: "inactive",
        id: 3, status: "failed",
        id: 4, status: "inactive",
        id: 5, status: "failed",
        ];
        let requiredArray = [2,3,4];
        let newArray = candidates.filter(x=>requiredArray.includes(x.id))
        console.log(newArray);








        share|improve this answer












        Try this.






        const candidates = [
        id: 1, status: "active",
        id: 2, status: "inactive",
        id: 3, status: "failed",
        id: 4, status: "inactive",
        id: 5, status: "failed",
        ];
        let requiredArray = [2,3,4];
        let newArray = candidates.filter(x=>requiredArray.includes(x.id))
        console.log(newArray);








        const candidates = [
        id: 1, status: "active",
        id: 2, status: "inactive",
        id: 3, status: "failed",
        id: 4, status: "inactive",
        id: 5, status: "failed",
        ];
        let requiredArray = [2,3,4];
        let newArray = candidates.filter(x=>requiredArray.includes(x.id))
        console.log(newArray);





        const candidates = [
        id: 1, status: "active",
        id: 2, status: "inactive",
        id: 3, status: "failed",
        id: 4, status: "inactive",
        id: 5, status: "failed",
        ];
        let requiredArray = [2,3,4];
        let newArray = candidates.filter(x=>requiredArray.includes(x.id))
        console.log(newArray);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 20:04









        eag845

        878611




        878611



























            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%2f53269178%2fhow-to-get-an-array-of-unique-values-of-a-specific-key-provided-i-only-have-an-a%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







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3