Deep filtering in hierarchical object












0















I have a hierarchical javascript object, which I need to filter out some of its elements. I wonder if there is an easy way to do this.



For example I have an object actions as below. I want to filter out those that have an default property which is set to true.



let actions={
sql: [
{
content: 'aaa',
default: true
},
{
stmt: 'bbb'
},
{
stmt: 'ccc',
default: true
}],
email: [
{
content: 'xxx',
default: true
}]
}


So eventually I want to have something like:



let filteredActions={
sql: [
{
stmt: 'bbb'
}
],
email:
}


This how I try to solve my problem, but seems it does not work.



getNonDefaultActions = actions =>
map(actions, (actionListByType, key) => {
actionListByType.filter(action => !(action.defaultFlag === true))
});


It seems nothing has been filtered. Any solution is welcome. Additionally, if lodash provides something handy for this type of problem that would be great










share|improve this question



























    0















    I have a hierarchical javascript object, which I need to filter out some of its elements. I wonder if there is an easy way to do this.



    For example I have an object actions as below. I want to filter out those that have an default property which is set to true.



    let actions={
    sql: [
    {
    content: 'aaa',
    default: true
    },
    {
    stmt: 'bbb'
    },
    {
    stmt: 'ccc',
    default: true
    }],
    email: [
    {
    content: 'xxx',
    default: true
    }]
    }


    So eventually I want to have something like:



    let filteredActions={
    sql: [
    {
    stmt: 'bbb'
    }
    ],
    email:
    }


    This how I try to solve my problem, but seems it does not work.



    getNonDefaultActions = actions =>
    map(actions, (actionListByType, key) => {
    actionListByType.filter(action => !(action.defaultFlag === true))
    });


    It seems nothing has been filtered. Any solution is welcome. Additionally, if lodash provides something handy for this type of problem that would be great










    share|improve this question

























      0












      0








      0








      I have a hierarchical javascript object, which I need to filter out some of its elements. I wonder if there is an easy way to do this.



      For example I have an object actions as below. I want to filter out those that have an default property which is set to true.



      let actions={
      sql: [
      {
      content: 'aaa',
      default: true
      },
      {
      stmt: 'bbb'
      },
      {
      stmt: 'ccc',
      default: true
      }],
      email: [
      {
      content: 'xxx',
      default: true
      }]
      }


      So eventually I want to have something like:



      let filteredActions={
      sql: [
      {
      stmt: 'bbb'
      }
      ],
      email:
      }


      This how I try to solve my problem, but seems it does not work.



      getNonDefaultActions = actions =>
      map(actions, (actionListByType, key) => {
      actionListByType.filter(action => !(action.defaultFlag === true))
      });


      It seems nothing has been filtered. Any solution is welcome. Additionally, if lodash provides something handy for this type of problem that would be great










      share|improve this question














      I have a hierarchical javascript object, which I need to filter out some of its elements. I wonder if there is an easy way to do this.



      For example I have an object actions as below. I want to filter out those that have an default property which is set to true.



      let actions={
      sql: [
      {
      content: 'aaa',
      default: true
      },
      {
      stmt: 'bbb'
      },
      {
      stmt: 'ccc',
      default: true
      }],
      email: [
      {
      content: 'xxx',
      default: true
      }]
      }


      So eventually I want to have something like:



      let filteredActions={
      sql: [
      {
      stmt: 'bbb'
      }
      ],
      email:
      }


      This how I try to solve my problem, but seems it does not work.



      getNonDefaultActions = actions =>
      map(actions, (actionListByType, key) => {
      actionListByType.filter(action => !(action.defaultFlag === true))
      });


      It seems nothing has been filtered. Any solution is welcome. Additionally, if lodash provides something handy for this type of problem that would be great







      javascript node.js lodash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 18:54









      RobertRobert

      52




      52
























          3 Answers
          3






          active

          oldest

          votes


















          0














          You can use lodash's _.mapValue() with _.reject():






          const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}

          const result = _.mapValues(actions, arr => _.reject(arr, 'default'))

          console.log(result)

          <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








          share|improve this answer































            0














            Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default)); does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.






            const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};

            Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));

            console.log(actions);








            share|improve this answer































              0














              You could get a new object with filtered properties.






              let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
              result = Object.assign(
              ...Object
              .entries(actions)
              .map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
              );

              console.log(result);








              share|improve this answer
























              • Happy new year :)

                – Ori Drori
                Jan 1 at 19:25











              • @OriDrori, happy new year to you, too :)

                – Nina Scholz
                Jan 1 at 20:47











              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%2f53998079%2fdeep-filtering-in-hierarchical-object%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You can use lodash's _.mapValue() with _.reject():






              const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}

              const result = _.mapValues(actions, arr => _.reject(arr, 'default'))

              console.log(result)

              <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








              share|improve this answer




























                0














                You can use lodash's _.mapValue() with _.reject():






                const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}

                const result = _.mapValues(actions, arr => _.reject(arr, 'default'))

                console.log(result)

                <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








                share|improve this answer


























                  0












                  0








                  0







                  You can use lodash's _.mapValue() with _.reject():






                  const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}

                  const result = _.mapValues(actions, arr => _.reject(arr, 'default'))

                  console.log(result)

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








                  share|improve this answer













                  You can use lodash's _.mapValue() with _.reject():






                  const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}

                  const result = _.mapValues(actions, arr => _.reject(arr, 'default'))

                  console.log(result)

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








                  const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}

                  const result = _.mapValues(actions, arr => _.reject(arr, 'default'))

                  console.log(result)

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>





                  const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]}

                  const result = _.mapValues(actions, arr => _.reject(arr, 'default'))

                  console.log(result)

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 18:57









                  Ori DroriOri Drori

                  79.6k138797




                  79.6k138797

























                      0














                      Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default)); does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.






                      const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};

                      Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));

                      console.log(actions);








                      share|improve this answer




























                        0














                        Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default)); does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.






                        const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};

                        Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));

                        console.log(actions);








                        share|improve this answer


























                          0












                          0








                          0







                          Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default)); does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.






                          const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};

                          Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));

                          console.log(actions);








                          share|improve this answer













                          Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default)); does the trick in vanilla. The idea is to iterate through key-value object pairs and reassign each value to a filtered version of itself.






                          const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};

                          Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));

                          console.log(actions);








                          const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};

                          Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));

                          console.log(actions);





                          const actions = {"sql":[{"content":"aaa","default":true},{"stmt":"bbb"},{"stmt":"ccc","default":true}],"email":[{"content":"xxx","default":true}]};

                          Object.entries(actions).forEach(([k, v]) => actions[k] = v.filter(f => !f.default));

                          console.log(actions);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 1 at 19:04









                          ggorlenggorlen

                          7,5033826




                          7,5033826























                              0














                              You could get a new object with filtered properties.






                              let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
                              result = Object.assign(
                              ...Object
                              .entries(actions)
                              .map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
                              );

                              console.log(result);








                              share|improve this answer
























                              • Happy new year :)

                                – Ori Drori
                                Jan 1 at 19:25











                              • @OriDrori, happy new year to you, too :)

                                – Nina Scholz
                                Jan 1 at 20:47
















                              0














                              You could get a new object with filtered properties.






                              let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
                              result = Object.assign(
                              ...Object
                              .entries(actions)
                              .map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
                              );

                              console.log(result);








                              share|improve this answer
























                              • Happy new year :)

                                – Ori Drori
                                Jan 1 at 19:25











                              • @OriDrori, happy new year to you, too :)

                                – Nina Scholz
                                Jan 1 at 20:47














                              0












                              0








                              0







                              You could get a new object with filtered properties.






                              let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
                              result = Object.assign(
                              ...Object
                              .entries(actions)
                              .map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
                              );

                              console.log(result);








                              share|improve this answer













                              You could get a new object with filtered properties.






                              let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
                              result = Object.assign(
                              ...Object
                              .entries(actions)
                              .map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
                              );

                              console.log(result);








                              let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
                              result = Object.assign(
                              ...Object
                              .entries(actions)
                              .map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
                              );

                              console.log(result);





                              let actions = { sql: [{ content: 'aaa', default: true }, { stmt: 'bbb' }, { stmt: 'ccc', default: true }], email: [{ content: 'xxx', default: true }] },
                              result = Object.assign(
                              ...Object
                              .entries(actions)
                              .map(([k, v]) => ({ [k]: v.filter(o => o.default !== true) }))
                              );

                              console.log(result);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 1 at 19:05









                              Nina ScholzNina Scholz

                              191k15100174




                              191k15100174













                              • Happy new year :)

                                – Ori Drori
                                Jan 1 at 19:25











                              • @OriDrori, happy new year to you, too :)

                                – Nina Scholz
                                Jan 1 at 20:47



















                              • Happy new year :)

                                – Ori Drori
                                Jan 1 at 19:25











                              • @OriDrori, happy new year to you, too :)

                                – Nina Scholz
                                Jan 1 at 20:47

















                              Happy new year :)

                              – Ori Drori
                              Jan 1 at 19:25





                              Happy new year :)

                              – Ori Drori
                              Jan 1 at 19:25













                              @OriDrori, happy new year to you, too :)

                              – Nina Scholz
                              Jan 1 at 20:47





                              @OriDrori, happy new year to you, too :)

                              – Nina Scholz
                              Jan 1 at 20:47


















                              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%2f53998079%2fdeep-filtering-in-hierarchical-object%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

                              MongoDB - Not Authorized To Execute Command

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                              How to fix TextFormField cause rebuild widget in Flutter