Performing delete operations on an immutable JS Map object












0















I have an immutable JS map with a structure like the below



{
"a": [
{"name": "foo", "untracked": true},
{"name": "bar"}
],
"b": [
{"name": "baz"},
{"name": "bar", "untracked": true}
]
}


I want to filter this object to only show objects which are tracked - i.e



{
"a": [
{"name": "bar"}
],
"b": [
{"name": "baz"},
]
}


Is there a way for me to do this with immutable map operations? i tried the below but it doesnt seem to work



object.map((lis) => lis.filter((li) => li.untracked !== true)).toJS()

object.toList().map((lis) => lis.filter((li) => li.untracked !== true))









share|improve this question



























    0















    I have an immutable JS map with a structure like the below



    {
    "a": [
    {"name": "foo", "untracked": true},
    {"name": "bar"}
    ],
    "b": [
    {"name": "baz"},
    {"name": "bar", "untracked": true}
    ]
    }


    I want to filter this object to only show objects which are tracked - i.e



    {
    "a": [
    {"name": "bar"}
    ],
    "b": [
    {"name": "baz"},
    ]
    }


    Is there a way for me to do this with immutable map operations? i tried the below but it doesnt seem to work



    object.map((lis) => lis.filter((li) => li.untracked !== true)).toJS()

    object.toList().map((lis) => lis.filter((li) => li.untracked !== true))









    share|improve this question

























      0












      0








      0


      1






      I have an immutable JS map with a structure like the below



      {
      "a": [
      {"name": "foo", "untracked": true},
      {"name": "bar"}
      ],
      "b": [
      {"name": "baz"},
      {"name": "bar", "untracked": true}
      ]
      }


      I want to filter this object to only show objects which are tracked - i.e



      {
      "a": [
      {"name": "bar"}
      ],
      "b": [
      {"name": "baz"},
      ]
      }


      Is there a way for me to do this with immutable map operations? i tried the below but it doesnt seem to work



      object.map((lis) => lis.filter((li) => li.untracked !== true)).toJS()

      object.toList().map((lis) => lis.filter((li) => li.untracked !== true))









      share|improve this question














      I have an immutable JS map with a structure like the below



      {
      "a": [
      {"name": "foo", "untracked": true},
      {"name": "bar"}
      ],
      "b": [
      {"name": "baz"},
      {"name": "bar", "untracked": true}
      ]
      }


      I want to filter this object to only show objects which are tracked - i.e



      {
      "a": [
      {"name": "bar"}
      ],
      "b": [
      {"name": "baz"},
      ]
      }


      Is there a way for me to do this with immutable map operations? i tried the below but it doesnt seem to work



      object.map((lis) => lis.filter((li) => li.untracked !== true)).toJS()

      object.toList().map((lis) => lis.filter((li) => li.untracked !== true))






      javascript immutable.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 16:30









      KannajKannaj

      2,04521641




      2,04521641
























          4 Answers
          4






          active

          oldest

          votes


















          1

















          let data={
          "a": [
          {"name": "foo", "untracked": true},
          {"name": "bar"}
          ],
          "b": [
          {"name": "baz"},
          {"name": "bar", "untracked": true}
          ]
          }
          Object.entries(data).map(([key, value]) => {
          data[key]=value.filter(ele=>ele.untracked!=true)
          });
          console.log("result is : ",data)








          share|improve this answer

































            0














            As per ImmutableJS docs:



            filter() returns a new Map with only the entries for which the predicate function returns true.


            example:



            function someFilterFunction(entry) {
            return !entry.untracked;
            }
            myMapOfStuff = myMapOfStuff.filter(someFilterFunction);


            Link to documentation: https://facebook.github.io/immutable-js/docs/#/Map/filter



            EDIT: Remember, the method on an immutable collection returns a COPY of the collection you are operating on. A reassignment must happen if you want to preserve the most up to date reference.






            share|improve this answer































              0














              Loop through that object and create a new object with keys from old object and then filter the values from the old object where untracked is not equal to false






              let obj = {
              "a": [{
              "name": "foo",
              "untracked": true
              },
              {
              "name": "bar"
              }
              ],
              "b": [{
              "name": "baz"
              },
              {
              "name": "bar",
              "untracked": true
              }
              ]
              }
              let newObj = {};

              function filterObject(obj) {
              for (let keys in obj) {
              newObj[keys] = obj[keys].filter(item => {
              return item.untracked !== true
              })
              }
              }
              filterObject(obj)
              console.log(newObj)








              share|improve this answer































                0

















                data = {
                "a": [
                {"name": "foo", "untracked": true},
                {"name": "bar"}
                ],
                "b": [
                {"name": "baz"},
                {"name": "bar", "untracked": true}
                ]
                }

                let result = Object.entries(data).map(([key, values])=>(
                { key: values.filter(({untracked})=> !untracked) }
                ))

                console.log(result)








                share|improve this answer


























                • Uncaught ReferenceError: result is not defined. Also you are changing the structure of the given data as output will be=> [ { "key": [ { "name": "bar" } ] }, { "key": [ { "name": "baz" } ] } ]

                  – Monica Acha
                  Nov 20 '18 at 17:40













                • I forgot to declare the result variable.Thanks @Monic

                  – Mohammed Ashfaq
                  Nov 21 '18 at 1:23











                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%2f53397409%2fperforming-delete-operations-on-an-immutable-js-map-object%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

















                let data={
                "a": [
                {"name": "foo", "untracked": true},
                {"name": "bar"}
                ],
                "b": [
                {"name": "baz"},
                {"name": "bar", "untracked": true}
                ]
                }
                Object.entries(data).map(([key, value]) => {
                data[key]=value.filter(ele=>ele.untracked!=true)
                });
                console.log("result is : ",data)








                share|improve this answer






























                  1

















                  let data={
                  "a": [
                  {"name": "foo", "untracked": true},
                  {"name": "bar"}
                  ],
                  "b": [
                  {"name": "baz"},
                  {"name": "bar", "untracked": true}
                  ]
                  }
                  Object.entries(data).map(([key, value]) => {
                  data[key]=value.filter(ele=>ele.untracked!=true)
                  });
                  console.log("result is : ",data)








                  share|improve this answer




























                    1












                    1








                    1










                    let data={
                    "a": [
                    {"name": "foo", "untracked": true},
                    {"name": "bar"}
                    ],
                    "b": [
                    {"name": "baz"},
                    {"name": "bar", "untracked": true}
                    ]
                    }
                    Object.entries(data).map(([key, value]) => {
                    data[key]=value.filter(ele=>ele.untracked!=true)
                    });
                    console.log("result is : ",data)








                    share|improve this answer


















                    let data={
                    "a": [
                    {"name": "foo", "untracked": true},
                    {"name": "bar"}
                    ],
                    "b": [
                    {"name": "baz"},
                    {"name": "bar", "untracked": true}
                    ]
                    }
                    Object.entries(data).map(([key, value]) => {
                    data[key]=value.filter(ele=>ele.untracked!=true)
                    });
                    console.log("result is : ",data)








                    let data={
                    "a": [
                    {"name": "foo", "untracked": true},
                    {"name": "bar"}
                    ],
                    "b": [
                    {"name": "baz"},
                    {"name": "bar", "untracked": true}
                    ]
                    }
                    Object.entries(data).map(([key, value]) => {
                    data[key]=value.filter(ele=>ele.untracked!=true)
                    });
                    console.log("result is : ",data)





                    let data={
                    "a": [
                    {"name": "foo", "untracked": true},
                    {"name": "bar"}
                    ],
                    "b": [
                    {"name": "baz"},
                    {"name": "bar", "untracked": true}
                    ]
                    }
                    Object.entries(data).map(([key, value]) => {
                    data[key]=value.filter(ele=>ele.untracked!=true)
                    });
                    console.log("result is : ",data)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 20 '18 at 17:05

























                    answered Nov 20 '18 at 16:59









                    BittuSBittuS

                    587216




                    587216

























                        0














                        As per ImmutableJS docs:



                        filter() returns a new Map with only the entries for which the predicate function returns true.


                        example:



                        function someFilterFunction(entry) {
                        return !entry.untracked;
                        }
                        myMapOfStuff = myMapOfStuff.filter(someFilterFunction);


                        Link to documentation: https://facebook.github.io/immutable-js/docs/#/Map/filter



                        EDIT: Remember, the method on an immutable collection returns a COPY of the collection you are operating on. A reassignment must happen if you want to preserve the most up to date reference.






                        share|improve this answer




























                          0














                          As per ImmutableJS docs:



                          filter() returns a new Map with only the entries for which the predicate function returns true.


                          example:



                          function someFilterFunction(entry) {
                          return !entry.untracked;
                          }
                          myMapOfStuff = myMapOfStuff.filter(someFilterFunction);


                          Link to documentation: https://facebook.github.io/immutable-js/docs/#/Map/filter



                          EDIT: Remember, the method on an immutable collection returns a COPY of the collection you are operating on. A reassignment must happen if you want to preserve the most up to date reference.






                          share|improve this answer


























                            0












                            0








                            0







                            As per ImmutableJS docs:



                            filter() returns a new Map with only the entries for which the predicate function returns true.


                            example:



                            function someFilterFunction(entry) {
                            return !entry.untracked;
                            }
                            myMapOfStuff = myMapOfStuff.filter(someFilterFunction);


                            Link to documentation: https://facebook.github.io/immutable-js/docs/#/Map/filter



                            EDIT: Remember, the method on an immutable collection returns a COPY of the collection you are operating on. A reassignment must happen if you want to preserve the most up to date reference.






                            share|improve this answer













                            As per ImmutableJS docs:



                            filter() returns a new Map with only the entries for which the predicate function returns true.


                            example:



                            function someFilterFunction(entry) {
                            return !entry.untracked;
                            }
                            myMapOfStuff = myMapOfStuff.filter(someFilterFunction);


                            Link to documentation: https://facebook.github.io/immutable-js/docs/#/Map/filter



                            EDIT: Remember, the method on an immutable collection returns a COPY of the collection you are operating on. A reassignment must happen if you want to preserve the most up to date reference.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 20 '18 at 16:35









                            Francisco GarciaFrancisco Garcia

                            704




                            704























                                0














                                Loop through that object and create a new object with keys from old object and then filter the values from the old object where untracked is not equal to false






                                let obj = {
                                "a": [{
                                "name": "foo",
                                "untracked": true
                                },
                                {
                                "name": "bar"
                                }
                                ],
                                "b": [{
                                "name": "baz"
                                },
                                {
                                "name": "bar",
                                "untracked": true
                                }
                                ]
                                }
                                let newObj = {};

                                function filterObject(obj) {
                                for (let keys in obj) {
                                newObj[keys] = obj[keys].filter(item => {
                                return item.untracked !== true
                                })
                                }
                                }
                                filterObject(obj)
                                console.log(newObj)








                                share|improve this answer




























                                  0














                                  Loop through that object and create a new object with keys from old object and then filter the values from the old object where untracked is not equal to false






                                  let obj = {
                                  "a": [{
                                  "name": "foo",
                                  "untracked": true
                                  },
                                  {
                                  "name": "bar"
                                  }
                                  ],
                                  "b": [{
                                  "name": "baz"
                                  },
                                  {
                                  "name": "bar",
                                  "untracked": true
                                  }
                                  ]
                                  }
                                  let newObj = {};

                                  function filterObject(obj) {
                                  for (let keys in obj) {
                                  newObj[keys] = obj[keys].filter(item => {
                                  return item.untracked !== true
                                  })
                                  }
                                  }
                                  filterObject(obj)
                                  console.log(newObj)








                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Loop through that object and create a new object with keys from old object and then filter the values from the old object where untracked is not equal to false






                                    let obj = {
                                    "a": [{
                                    "name": "foo",
                                    "untracked": true
                                    },
                                    {
                                    "name": "bar"
                                    }
                                    ],
                                    "b": [{
                                    "name": "baz"
                                    },
                                    {
                                    "name": "bar",
                                    "untracked": true
                                    }
                                    ]
                                    }
                                    let newObj = {};

                                    function filterObject(obj) {
                                    for (let keys in obj) {
                                    newObj[keys] = obj[keys].filter(item => {
                                    return item.untracked !== true
                                    })
                                    }
                                    }
                                    filterObject(obj)
                                    console.log(newObj)








                                    share|improve this answer













                                    Loop through that object and create a new object with keys from old object and then filter the values from the old object where untracked is not equal to false






                                    let obj = {
                                    "a": [{
                                    "name": "foo",
                                    "untracked": true
                                    },
                                    {
                                    "name": "bar"
                                    }
                                    ],
                                    "b": [{
                                    "name": "baz"
                                    },
                                    {
                                    "name": "bar",
                                    "untracked": true
                                    }
                                    ]
                                    }
                                    let newObj = {};

                                    function filterObject(obj) {
                                    for (let keys in obj) {
                                    newObj[keys] = obj[keys].filter(item => {
                                    return item.untracked !== true
                                    })
                                    }
                                    }
                                    filterObject(obj)
                                    console.log(newObj)








                                    let obj = {
                                    "a": [{
                                    "name": "foo",
                                    "untracked": true
                                    },
                                    {
                                    "name": "bar"
                                    }
                                    ],
                                    "b": [{
                                    "name": "baz"
                                    },
                                    {
                                    "name": "bar",
                                    "untracked": true
                                    }
                                    ]
                                    }
                                    let newObj = {};

                                    function filterObject(obj) {
                                    for (let keys in obj) {
                                    newObj[keys] = obj[keys].filter(item => {
                                    return item.untracked !== true
                                    })
                                    }
                                    }
                                    filterObject(obj)
                                    console.log(newObj)





                                    let obj = {
                                    "a": [{
                                    "name": "foo",
                                    "untracked": true
                                    },
                                    {
                                    "name": "bar"
                                    }
                                    ],
                                    "b": [{
                                    "name": "baz"
                                    },
                                    {
                                    "name": "bar",
                                    "untracked": true
                                    }
                                    ]
                                    }
                                    let newObj = {};

                                    function filterObject(obj) {
                                    for (let keys in obj) {
                                    newObj[keys] = obj[keys].filter(item => {
                                    return item.untracked !== true
                                    })
                                    }
                                    }
                                    filterObject(obj)
                                    console.log(newObj)






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 20 '18 at 16:37









                                    brkbrk

                                    26.5k31940




                                    26.5k31940























                                        0

















                                        data = {
                                        "a": [
                                        {"name": "foo", "untracked": true},
                                        {"name": "bar"}
                                        ],
                                        "b": [
                                        {"name": "baz"},
                                        {"name": "bar", "untracked": true}
                                        ]
                                        }

                                        let result = Object.entries(data).map(([key, values])=>(
                                        { key: values.filter(({untracked})=> !untracked) }
                                        ))

                                        console.log(result)








                                        share|improve this answer


























                                        • Uncaught ReferenceError: result is not defined. Also you are changing the structure of the given data as output will be=> [ { "key": [ { "name": "bar" } ] }, { "key": [ { "name": "baz" } ] } ]

                                          – Monica Acha
                                          Nov 20 '18 at 17:40













                                        • I forgot to declare the result variable.Thanks @Monic

                                          – Mohammed Ashfaq
                                          Nov 21 '18 at 1:23
















                                        0

















                                        data = {
                                        "a": [
                                        {"name": "foo", "untracked": true},
                                        {"name": "bar"}
                                        ],
                                        "b": [
                                        {"name": "baz"},
                                        {"name": "bar", "untracked": true}
                                        ]
                                        }

                                        let result = Object.entries(data).map(([key, values])=>(
                                        { key: values.filter(({untracked})=> !untracked) }
                                        ))

                                        console.log(result)








                                        share|improve this answer


























                                        • Uncaught ReferenceError: result is not defined. Also you are changing the structure of the given data as output will be=> [ { "key": [ { "name": "bar" } ] }, { "key": [ { "name": "baz" } ] } ]

                                          – Monica Acha
                                          Nov 20 '18 at 17:40













                                        • I forgot to declare the result variable.Thanks @Monic

                                          – Mohammed Ashfaq
                                          Nov 21 '18 at 1:23














                                        0












                                        0








                                        0










                                        data = {
                                        "a": [
                                        {"name": "foo", "untracked": true},
                                        {"name": "bar"}
                                        ],
                                        "b": [
                                        {"name": "baz"},
                                        {"name": "bar", "untracked": true}
                                        ]
                                        }

                                        let result = Object.entries(data).map(([key, values])=>(
                                        { key: values.filter(({untracked})=> !untracked) }
                                        ))

                                        console.log(result)








                                        share|improve this answer


















                                        data = {
                                        "a": [
                                        {"name": "foo", "untracked": true},
                                        {"name": "bar"}
                                        ],
                                        "b": [
                                        {"name": "baz"},
                                        {"name": "bar", "untracked": true}
                                        ]
                                        }

                                        let result = Object.entries(data).map(([key, values])=>(
                                        { key: values.filter(({untracked})=> !untracked) }
                                        ))

                                        console.log(result)








                                        data = {
                                        "a": [
                                        {"name": "foo", "untracked": true},
                                        {"name": "bar"}
                                        ],
                                        "b": [
                                        {"name": "baz"},
                                        {"name": "bar", "untracked": true}
                                        ]
                                        }

                                        let result = Object.entries(data).map(([key, values])=>(
                                        { key: values.filter(({untracked})=> !untracked) }
                                        ))

                                        console.log(result)





                                        data = {
                                        "a": [
                                        {"name": "foo", "untracked": true},
                                        {"name": "bar"}
                                        ],
                                        "b": [
                                        {"name": "baz"},
                                        {"name": "bar", "untracked": true}
                                        ]
                                        }

                                        let result = Object.entries(data).map(([key, values])=>(
                                        { key: values.filter(({untracked})=> !untracked) }
                                        ))

                                        console.log(result)






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Nov 21 '18 at 1:21

























                                        answered Nov 20 '18 at 17:14









                                        Mohammed AshfaqMohammed Ashfaq

                                        1,6672612




                                        1,6672612













                                        • Uncaught ReferenceError: result is not defined. Also you are changing the structure of the given data as output will be=> [ { "key": [ { "name": "bar" } ] }, { "key": [ { "name": "baz" } ] } ]

                                          – Monica Acha
                                          Nov 20 '18 at 17:40













                                        • I forgot to declare the result variable.Thanks @Monic

                                          – Mohammed Ashfaq
                                          Nov 21 '18 at 1:23



















                                        • Uncaught ReferenceError: result is not defined. Also you are changing the structure of the given data as output will be=> [ { "key": [ { "name": "bar" } ] }, { "key": [ { "name": "baz" } ] } ]

                                          – Monica Acha
                                          Nov 20 '18 at 17:40













                                        • I forgot to declare the result variable.Thanks @Monic

                                          – Mohammed Ashfaq
                                          Nov 21 '18 at 1:23

















                                        Uncaught ReferenceError: result is not defined. Also you are changing the structure of the given data as output will be=> [ { "key": [ { "name": "bar" } ] }, { "key": [ { "name": "baz" } ] } ]

                                        – Monica Acha
                                        Nov 20 '18 at 17:40







                                        Uncaught ReferenceError: result is not defined. Also you are changing the structure of the given data as output will be=> [ { "key": [ { "name": "bar" } ] }, { "key": [ { "name": "baz" } ] } ]

                                        – Monica Acha
                                        Nov 20 '18 at 17:40















                                        I forgot to declare the result variable.Thanks @Monic

                                        – Mohammed Ashfaq
                                        Nov 21 '18 at 1:23





                                        I forgot to declare the result variable.Thanks @Monic

                                        – Mohammed Ashfaq
                                        Nov 21 '18 at 1:23


















                                        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%2f53397409%2fperforming-delete-operations-on-an-immutable-js-map-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