Map keys into array according to values





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















what's the most efficient way to map keys into array according to values based on the condition of the value?



For example, I have a map that contains the File object as the key and boolean as the value like this:



map = new Map([
[file, true],
[file1, true],
[file2, false],
[file3, true],
]);


Can I ask what's the shortcut way of creating an array of file objects if map value === true?



Intended outcome



files = [file,file1,file3];



Appreciate your help thanks!










share|improve this question





























    0















    what's the most efficient way to map keys into array according to values based on the condition of the value?



    For example, I have a map that contains the File object as the key and boolean as the value like this:



    map = new Map([
    [file, true],
    [file1, true],
    [file2, false],
    [file3, true],
    ]);


    Can I ask what's the shortcut way of creating an array of file objects if map value === true?



    Intended outcome



    files = [file,file1,file3];



    Appreciate your help thanks!










    share|improve this question

























      0












      0








      0








      what's the most efficient way to map keys into array according to values based on the condition of the value?



      For example, I have a map that contains the File object as the key and boolean as the value like this:



      map = new Map([
      [file, true],
      [file1, true],
      [file2, false],
      [file3, true],
      ]);


      Can I ask what's the shortcut way of creating an array of file objects if map value === true?



      Intended outcome



      files = [file,file1,file3];



      Appreciate your help thanks!










      share|improve this question














      what's the most efficient way to map keys into array according to values based on the condition of the value?



      For example, I have a map that contains the File object as the key and boolean as the value like this:



      map = new Map([
      [file, true],
      [file1, true],
      [file2, false],
      [file3, true],
      ]);


      Can I ask what's the shortcut way of creating an array of file objects if map value === true?



      Intended outcome



      files = [file,file1,file3];



      Appreciate your help thanks!







      javascript typescript dictionary angular6






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 2:58









      iBlehhziBlehhz

      1229




      1229
























          4 Answers
          4






          active

          oldest

          votes


















          1














          You can use entries to get the keys and the values of the map, then filter it by whether a value (the condition) is true, and then .map to turn the [key, value] into just the key:






          const map = new Map([
          ['file', true],
          ['file1', true],
          ['file2', false],
          ['file3', true],
          ]);
          const files = [...map.entries()]
          .filter(([_, cond]) => cond)
          .map(([key]) => key);
          console.log(files);








          share|improve this answer































            1














            A simple forEach loop could do the trick here.






            const map = new Map([
            ['test', true],
            ['test2', true],
            ['test3', false],
            ['test4', true],
            ]);

            const files = ;

            map.forEach((value, key, map) => {
            if (value) {
            files.push(key)
            }

            });
            console.log(files);








            share|improve this answer































              1














              You could use Map.entries with filter and map to achieve this. The basic idea is




              1. Get an array of all keys/values with Map.entries

              2. filter that array to only the entries with the value true

              3. use map to convert from Map entries to array elements.


              the code would look something like this.



              map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);





              share|improve this answer































                1














                The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods



                aka:



                myMap = new Map([
                ["file", true],
                ["file1", true],
                ["file2", false],
                ["file3", true],
                ]);

                const fastest = (files) => {
                const map = [...files]
                const out =
                for (let i=0; i<map.length; i++){
                if (map[i][1]){
                out.push(map[i][0])
                }
                }
                return out
                }

                console.log(fastest(myMap))


                https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
                There are many articles and a lot of literature about this if you have a look around






                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%2f54015767%2fmap-keys-into-array-according-to-values%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  1














                  You can use entries to get the keys and the values of the map, then filter it by whether a value (the condition) is true, and then .map to turn the [key, value] into just the key:






                  const map = new Map([
                  ['file', true],
                  ['file1', true],
                  ['file2', false],
                  ['file3', true],
                  ]);
                  const files = [...map.entries()]
                  .filter(([_, cond]) => cond)
                  .map(([key]) => key);
                  console.log(files);








                  share|improve this answer




























                    1














                    You can use entries to get the keys and the values of the map, then filter it by whether a value (the condition) is true, and then .map to turn the [key, value] into just the key:






                    const map = new Map([
                    ['file', true],
                    ['file1', true],
                    ['file2', false],
                    ['file3', true],
                    ]);
                    const files = [...map.entries()]
                    .filter(([_, cond]) => cond)
                    .map(([key]) => key);
                    console.log(files);








                    share|improve this answer


























                      1












                      1








                      1







                      You can use entries to get the keys and the values of the map, then filter it by whether a value (the condition) is true, and then .map to turn the [key, value] into just the key:






                      const map = new Map([
                      ['file', true],
                      ['file1', true],
                      ['file2', false],
                      ['file3', true],
                      ]);
                      const files = [...map.entries()]
                      .filter(([_, cond]) => cond)
                      .map(([key]) => key);
                      console.log(files);








                      share|improve this answer













                      You can use entries to get the keys and the values of the map, then filter it by whether a value (the condition) is true, and then .map to turn the [key, value] into just the key:






                      const map = new Map([
                      ['file', true],
                      ['file1', true],
                      ['file2', false],
                      ['file3', true],
                      ]);
                      const files = [...map.entries()]
                      .filter(([_, cond]) => cond)
                      .map(([key]) => key);
                      console.log(files);








                      const map = new Map([
                      ['file', true],
                      ['file1', true],
                      ['file2', false],
                      ['file3', true],
                      ]);
                      const files = [...map.entries()]
                      .filter(([_, cond]) => cond)
                      .map(([key]) => key);
                      console.log(files);





                      const map = new Map([
                      ['file', true],
                      ['file1', true],
                      ['file2', false],
                      ['file3', true],
                      ]);
                      const files = [...map.entries()]
                      .filter(([_, cond]) => cond)
                      .map(([key]) => key);
                      console.log(files);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 3 at 3:05









                      CertainPerformanceCertainPerformance

                      97.5k165887




                      97.5k165887

























                          1














                          A simple forEach loop could do the trick here.






                          const map = new Map([
                          ['test', true],
                          ['test2', true],
                          ['test3', false],
                          ['test4', true],
                          ]);

                          const files = ;

                          map.forEach((value, key, map) => {
                          if (value) {
                          files.push(key)
                          }

                          });
                          console.log(files);








                          share|improve this answer




























                            1














                            A simple forEach loop could do the trick here.






                            const map = new Map([
                            ['test', true],
                            ['test2', true],
                            ['test3', false],
                            ['test4', true],
                            ]);

                            const files = ;

                            map.forEach((value, key, map) => {
                            if (value) {
                            files.push(key)
                            }

                            });
                            console.log(files);








                            share|improve this answer


























                              1












                              1








                              1







                              A simple forEach loop could do the trick here.






                              const map = new Map([
                              ['test', true],
                              ['test2', true],
                              ['test3', false],
                              ['test4', true],
                              ]);

                              const files = ;

                              map.forEach((value, key, map) => {
                              if (value) {
                              files.push(key)
                              }

                              });
                              console.log(files);








                              share|improve this answer













                              A simple forEach loop could do the trick here.






                              const map = new Map([
                              ['test', true],
                              ['test2', true],
                              ['test3', false],
                              ['test4', true],
                              ]);

                              const files = ;

                              map.forEach((value, key, map) => {
                              if (value) {
                              files.push(key)
                              }

                              });
                              console.log(files);








                              const map = new Map([
                              ['test', true],
                              ['test2', true],
                              ['test3', false],
                              ['test4', true],
                              ]);

                              const files = ;

                              map.forEach((value, key, map) => {
                              if (value) {
                              files.push(key)
                              }

                              });
                              console.log(files);





                              const map = new Map([
                              ['test', true],
                              ['test2', true],
                              ['test3', false],
                              ['test4', true],
                              ]);

                              const files = ;

                              map.forEach((value, key, map) => {
                              if (value) {
                              files.push(key)
                              }

                              });
                              console.log(files);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 3 at 3:06









                              XzandroXzandro

                              661410




                              661410























                                  1














                                  You could use Map.entries with filter and map to achieve this. The basic idea is




                                  1. Get an array of all keys/values with Map.entries

                                  2. filter that array to only the entries with the value true

                                  3. use map to convert from Map entries to array elements.


                                  the code would look something like this.



                                  map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);





                                  share|improve this answer




























                                    1














                                    You could use Map.entries with filter and map to achieve this. The basic idea is




                                    1. Get an array of all keys/values with Map.entries

                                    2. filter that array to only the entries with the value true

                                    3. use map to convert from Map entries to array elements.


                                    the code would look something like this.



                                    map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);





                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      You could use Map.entries with filter and map to achieve this. The basic idea is




                                      1. Get an array of all keys/values with Map.entries

                                      2. filter that array to only the entries with the value true

                                      3. use map to convert from Map entries to array elements.


                                      the code would look something like this.



                                      map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);





                                      share|improve this answer













                                      You could use Map.entries with filter and map to achieve this. The basic idea is




                                      1. Get an array of all keys/values with Map.entries

                                      2. filter that array to only the entries with the value true

                                      3. use map to convert from Map entries to array elements.


                                      the code would look something like this.



                                      map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 3 at 3:08









                                      schu34schu34

                                      644619




                                      644619























                                          1














                                          The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods



                                          aka:



                                          myMap = new Map([
                                          ["file", true],
                                          ["file1", true],
                                          ["file2", false],
                                          ["file3", true],
                                          ]);

                                          const fastest = (files) => {
                                          const map = [...files]
                                          const out =
                                          for (let i=0; i<map.length; i++){
                                          if (map[i][1]){
                                          out.push(map[i][0])
                                          }
                                          }
                                          return out
                                          }

                                          console.log(fastest(myMap))


                                          https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
                                          There are many articles and a lot of literature about this if you have a look around






                                          share|improve this answer




























                                            1














                                            The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods



                                            aka:



                                            myMap = new Map([
                                            ["file", true],
                                            ["file1", true],
                                            ["file2", false],
                                            ["file3", true],
                                            ]);

                                            const fastest = (files) => {
                                            const map = [...files]
                                            const out =
                                            for (let i=0; i<map.length; i++){
                                            if (map[i][1]){
                                            out.push(map[i][0])
                                            }
                                            }
                                            return out
                                            }

                                            console.log(fastest(myMap))


                                            https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
                                            There are many articles and a lot of literature about this if you have a look around






                                            share|improve this answer


























                                              1












                                              1








                                              1







                                              The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods



                                              aka:



                                              myMap = new Map([
                                              ["file", true],
                                              ["file1", true],
                                              ["file2", false],
                                              ["file3", true],
                                              ]);

                                              const fastest = (files) => {
                                              const map = [...files]
                                              const out =
                                              for (let i=0; i<map.length; i++){
                                              if (map[i][1]){
                                              out.push(map[i][0])
                                              }
                                              }
                                              return out
                                              }

                                              console.log(fastest(myMap))


                                              https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
                                              There are many articles and a lot of literature about this if you have a look around






                                              share|improve this answer













                                              The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods



                                              aka:



                                              myMap = new Map([
                                              ["file", true],
                                              ["file1", true],
                                              ["file2", false],
                                              ["file3", true],
                                              ]);

                                              const fastest = (files) => {
                                              const map = [...files]
                                              const out =
                                              for (let i=0; i<map.length; i++){
                                              if (map[i][1]){
                                              out.push(map[i][0])
                                              }
                                              }
                                              return out
                                              }

                                              console.log(fastest(myMap))


                                              https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
                                              There are many articles and a lot of literature about this if you have a look around







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jan 3 at 3:25









                                              Happy MachineHappy Machine

                                              452213




                                              452213






























                                                  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%2f54015767%2fmap-keys-into-array-according-to-values%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

                                                  'app-layout' is not a known element: how to share Component with different Modules

                                                  android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                                  WPF add header to Image with URL pettitions [duplicate]