Get Object Properties By Predefined Property Names Array Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Detecting an undefined object propertyWhat is the most efficient way to deep clone an object in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Sort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Iterate through object propertiesFor-each over an array in JavaScript?

Trying to understand entropy as a novice in thermodynamics

Google .dev domain strangely redirects to https

How many time has Arya actually used Needle?

How to change the tick of the color bar legend to black

Why is std::move not [[nodiscard]] in C++20?

What does it mean that physics no longer uses mechanical models to describe phenomena?

I can't produce songs

Is there hard evidence that the grant peer review system performs significantly better than random?

The test team as an enemy of development? And how can this be avoided?

What would you call this weird metallic apparatus that allows you to lift people?

What is the chair depicted in Cesare Maccari's 1889 painting "Cicerone denuncia Catilina"?

What does 丫 mean? 丫是什么意思?

How does light 'choose' between wave and particle behaviour?

Are the endpoints of the domain of a function counted as critical points?

Is openssl rand command cryptographically secure?

Random body shuffle every night—can we still function?

Is CEO the "profession" with the most psychopaths?

Printing attributes of selection in ArcPy?

Flight departed from the gate 5 min before scheduled departure time. Refund options

Why complex landing gears are used instead of simple,reliability and light weight muscle wire or shape memory alloys?

Sally's older brother

Is it dangerous to install hacking tools on my private linux machine?

Is multiple magic items in one inherently imbalanced?

In musical terms, what properties are varied by the human voice to produce different words / syllables?



Get Object Properties By Predefined Property Names Array



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Detecting an undefined object propertyWhat is the most efficient way to deep clone an object in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Sort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Iterate through object propertiesFor-each over an array in JavaScript?



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








0















I am pretty sure about this kind of question answered before, but I couldn't make good search.



I have an array of object like;



[
prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1"
prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2"
prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
]


And I have an array which is storing needed property names like;



[ "prop1", "prop2" ]


So I need to filter all of my objects in array with the property names which is given by another array. And the output will be like;



[ 
prop1:"foo1", prop2:"baz1"
prop1:"foo2", prop2:"baz2"
prop1:"foo3", prop2:"baz3"
]


How can I do this in proper way ?










share|improve this question




























    0















    I am pretty sure about this kind of question answered before, but I couldn't make good search.



    I have an array of object like;



    [
    prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1"
    prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2"
    prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
    ]


    And I have an array which is storing needed property names like;



    [ "prop1", "prop2" ]


    So I need to filter all of my objects in array with the property names which is given by another array. And the output will be like;



    [ 
    prop1:"foo1", prop2:"baz1"
    prop1:"foo2", prop2:"baz2"
    prop1:"foo3", prop2:"baz3"
    ]


    How can I do this in proper way ?










    share|improve this question
























      0












      0








      0








      I am pretty sure about this kind of question answered before, but I couldn't make good search.



      I have an array of object like;



      [
      prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1"
      prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2"
      prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
      ]


      And I have an array which is storing needed property names like;



      [ "prop1", "prop2" ]


      So I need to filter all of my objects in array with the property names which is given by another array. And the output will be like;



      [ 
      prop1:"foo1", prop2:"baz1"
      prop1:"foo2", prop2:"baz2"
      prop1:"foo3", prop2:"baz3"
      ]


      How can I do this in proper way ?










      share|improve this question














      I am pretty sure about this kind of question answered before, but I couldn't make good search.



      I have an array of object like;



      [
      prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1"
      prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2"
      prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
      ]


      And I have an array which is storing needed property names like;



      [ "prop1", "prop2" ]


      So I need to filter all of my objects in array with the property names which is given by another array. And the output will be like;



      [ 
      prop1:"foo1", prop2:"baz1"
      prop1:"foo2", prop2:"baz2"
      prop1:"foo3", prop2:"baz3"
      ]


      How can I do this in proper way ?







      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 11:21









      berkanberkan

      1401111




      1401111






















          4 Answers
          4






          active

          oldest

          votes


















          4














          You can use map() and reduce()






          const data = [
          prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
          prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
          prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
          ];

          const props = [ "prop1", "prop2" ];

          const res = data.map(e => props.reduce((a,c) => (a[c] = e[c] , a), ));

          console.log(res)








          share|improve this answer

























          • Nice solution, could you please use more explicit variable names for the sake of readability? I understand the point of having a one liner, but reduce's params are obscure enough (upvoting anyway)

            – axelduch
            Mar 22 at 11:30







          • 1





            @axelduch a is accumulator, c is current element. Docs outline what arguments are

            – charlietfl
            Mar 22 at 11:32






          • 1





            I was rather saying that for those unfamiliar with reduce. It doesn't have to be scary to them :) But fair enough - people curious enough about it will check the doc anyway

            – axelduch
            Mar 22 at 11:37


















          4














          You can create pick function with reduce method and then use it with map method.






          const data = [ prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1", prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2", prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"]
          const props = [ "prop1", "prop2" ]

          const pick = (o, p) => p.reduce((r, e) => Object.assign(r, [e]: o[e]), )
          const res = data.map(o => pick(o, props));
          console.log(res)








          share|improve this answer






























            3














            You can do something like below to achieve the result:






            const data = [
            prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
            prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
            prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3",
            ]

            const keys = [ "prop1", "prop2" ]

            let result = data.map((record) =>
            let obj =
            keys.forEach((key) =>
            obj[key] = record[key]
            )
            return obj
            )

            console.log(result)








            share|improve this answer






























              1

















              let arr = [
              prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
              prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
              prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
              ]

              let filter = [ "prop1", "prop2" ];

              let out = [...arr].map(e => Object.keys(e).map(k => !filter.includes(k) ? delete e[k] :true) && e);
              console.log(out)








              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%2f55298528%2fget-object-properties-by-predefined-property-names-array%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









                4














                You can use map() and reduce()






                const data = [
                prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                ];

                const props = [ "prop1", "prop2" ];

                const res = data.map(e => props.reduce((a,c) => (a[c] = e[c] , a), ));

                console.log(res)








                share|improve this answer

























                • Nice solution, could you please use more explicit variable names for the sake of readability? I understand the point of having a one liner, but reduce's params are obscure enough (upvoting anyway)

                  – axelduch
                  Mar 22 at 11:30







                • 1





                  @axelduch a is accumulator, c is current element. Docs outline what arguments are

                  – charlietfl
                  Mar 22 at 11:32






                • 1





                  I was rather saying that for those unfamiliar with reduce. It doesn't have to be scary to them :) But fair enough - people curious enough about it will check the doc anyway

                  – axelduch
                  Mar 22 at 11:37















                4














                You can use map() and reduce()






                const data = [
                prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                ];

                const props = [ "prop1", "prop2" ];

                const res = data.map(e => props.reduce((a,c) => (a[c] = e[c] , a), ));

                console.log(res)








                share|improve this answer

























                • Nice solution, could you please use more explicit variable names for the sake of readability? I understand the point of having a one liner, but reduce's params are obscure enough (upvoting anyway)

                  – axelduch
                  Mar 22 at 11:30







                • 1





                  @axelduch a is accumulator, c is current element. Docs outline what arguments are

                  – charlietfl
                  Mar 22 at 11:32






                • 1





                  I was rather saying that for those unfamiliar with reduce. It doesn't have to be scary to them :) But fair enough - people curious enough about it will check the doc anyway

                  – axelduch
                  Mar 22 at 11:37













                4












                4








                4







                You can use map() and reduce()






                const data = [
                prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                ];

                const props = [ "prop1", "prop2" ];

                const res = data.map(e => props.reduce((a,c) => (a[c] = e[c] , a), ));

                console.log(res)








                share|improve this answer















                You can use map() and reduce()






                const data = [
                prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                ];

                const props = [ "prop1", "prop2" ];

                const res = data.map(e => props.reduce((a,c) => (a[c] = e[c] , a), ));

                console.log(res)








                const data = [
                prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                ];

                const props = [ "prop1", "prop2" ];

                const res = data.map(e => props.reduce((a,c) => (a[c] = e[c] , a), ));

                console.log(res)





                const data = [
                prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                ];

                const props = [ "prop1", "prop2" ];

                const res = data.map(e => props.reduce((a,c) => (a[c] = e[c] , a), ));

                console.log(res)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 22 at 11:33

























                answered Mar 22 at 11:28









                charlietflcharlietfl

                143k1391126




                143k1391126












                • Nice solution, could you please use more explicit variable names for the sake of readability? I understand the point of having a one liner, but reduce's params are obscure enough (upvoting anyway)

                  – axelduch
                  Mar 22 at 11:30







                • 1





                  @axelduch a is accumulator, c is current element. Docs outline what arguments are

                  – charlietfl
                  Mar 22 at 11:32






                • 1





                  I was rather saying that for those unfamiliar with reduce. It doesn't have to be scary to them :) But fair enough - people curious enough about it will check the doc anyway

                  – axelduch
                  Mar 22 at 11:37

















                • Nice solution, could you please use more explicit variable names for the sake of readability? I understand the point of having a one liner, but reduce's params are obscure enough (upvoting anyway)

                  – axelduch
                  Mar 22 at 11:30







                • 1





                  @axelduch a is accumulator, c is current element. Docs outline what arguments are

                  – charlietfl
                  Mar 22 at 11:32






                • 1





                  I was rather saying that for those unfamiliar with reduce. It doesn't have to be scary to them :) But fair enough - people curious enough about it will check the doc anyway

                  – axelduch
                  Mar 22 at 11:37
















                Nice solution, could you please use more explicit variable names for the sake of readability? I understand the point of having a one liner, but reduce's params are obscure enough (upvoting anyway)

                – axelduch
                Mar 22 at 11:30






                Nice solution, could you please use more explicit variable names for the sake of readability? I understand the point of having a one liner, but reduce's params are obscure enough (upvoting anyway)

                – axelduch
                Mar 22 at 11:30





                1




                1





                @axelduch a is accumulator, c is current element. Docs outline what arguments are

                – charlietfl
                Mar 22 at 11:32





                @axelduch a is accumulator, c is current element. Docs outline what arguments are

                – charlietfl
                Mar 22 at 11:32




                1




                1





                I was rather saying that for those unfamiliar with reduce. It doesn't have to be scary to them :) But fair enough - people curious enough about it will check the doc anyway

                – axelduch
                Mar 22 at 11:37





                I was rather saying that for those unfamiliar with reduce. It doesn't have to be scary to them :) But fair enough - people curious enough about it will check the doc anyway

                – axelduch
                Mar 22 at 11:37













                4














                You can create pick function with reduce method and then use it with map method.






                const data = [ prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1", prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2", prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"]
                const props = [ "prop1", "prop2" ]

                const pick = (o, p) => p.reduce((r, e) => Object.assign(r, [e]: o[e]), )
                const res = data.map(o => pick(o, props));
                console.log(res)








                share|improve this answer



























                  4














                  You can create pick function with reduce method and then use it with map method.






                  const data = [ prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1", prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2", prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"]
                  const props = [ "prop1", "prop2" ]

                  const pick = (o, p) => p.reduce((r, e) => Object.assign(r, [e]: o[e]), )
                  const res = data.map(o => pick(o, props));
                  console.log(res)








                  share|improve this answer

























                    4












                    4








                    4







                    You can create pick function with reduce method and then use it with map method.






                    const data = [ prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1", prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2", prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"]
                    const props = [ "prop1", "prop2" ]

                    const pick = (o, p) => p.reduce((r, e) => Object.assign(r, [e]: o[e]), )
                    const res = data.map(o => pick(o, props));
                    console.log(res)








                    share|improve this answer













                    You can create pick function with reduce method and then use it with map method.






                    const data = [ prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1", prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2", prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"]
                    const props = [ "prop1", "prop2" ]

                    const pick = (o, p) => p.reduce((r, e) => Object.assign(r, [e]: o[e]), )
                    const res = data.map(o => pick(o, props));
                    console.log(res)








                    const data = [ prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1", prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2", prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"]
                    const props = [ "prop1", "prop2" ]

                    const pick = (o, p) => p.reduce((r, e) => Object.assign(r, [e]: o[e]), )
                    const res = data.map(o => pick(o, props));
                    console.log(res)





                    const data = [ prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1", prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2", prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"]
                    const props = [ "prop1", "prop2" ]

                    const pick = (o, p) => p.reduce((r, e) => Object.assign(r, [e]: o[e]), )
                    const res = data.map(o => pick(o, props));
                    console.log(res)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 22 at 11:26









                    Nenad VracarNenad Vracar

                    73.8k126085




                    73.8k126085





















                        3














                        You can do something like below to achieve the result:






                        const data = [
                        prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                        prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                        prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3",
                        ]

                        const keys = [ "prop1", "prop2" ]

                        let result = data.map((record) =>
                        let obj =
                        keys.forEach((key) =>
                        obj[key] = record[key]
                        )
                        return obj
                        )

                        console.log(result)








                        share|improve this answer



























                          3














                          You can do something like below to achieve the result:






                          const data = [
                          prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                          prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                          prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3",
                          ]

                          const keys = [ "prop1", "prop2" ]

                          let result = data.map((record) =>
                          let obj =
                          keys.forEach((key) =>
                          obj[key] = record[key]
                          )
                          return obj
                          )

                          console.log(result)








                          share|improve this answer

























                            3












                            3








                            3







                            You can do something like below to achieve the result:






                            const data = [
                            prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                            prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                            prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3",
                            ]

                            const keys = [ "prop1", "prop2" ]

                            let result = data.map((record) =>
                            let obj =
                            keys.forEach((key) =>
                            obj[key] = record[key]
                            )
                            return obj
                            )

                            console.log(result)








                            share|improve this answer













                            You can do something like below to achieve the result:






                            const data = [
                            prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                            prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                            prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3",
                            ]

                            const keys = [ "prop1", "prop2" ]

                            let result = data.map((record) =>
                            let obj =
                            keys.forEach((key) =>
                            obj[key] = record[key]
                            )
                            return obj
                            )

                            console.log(result)








                            const data = [
                            prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                            prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                            prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3",
                            ]

                            const keys = [ "prop1", "prop2" ]

                            let result = data.map((record) =>
                            let obj =
                            keys.forEach((key) =>
                            obj[key] = record[key]
                            )
                            return obj
                            )

                            console.log(result)





                            const data = [
                            prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                            prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                            prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3",
                            ]

                            const keys = [ "prop1", "prop2" ]

                            let result = data.map((record) =>
                            let obj =
                            keys.forEach((key) =>
                            obj[key] = record[key]
                            )
                            return obj
                            )

                            console.log(result)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 22 at 11:27









                            User97User97

                            1,58211435




                            1,58211435





















                                1

















                                let arr = [
                                prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                                prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                                prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                                ]

                                let filter = [ "prop1", "prop2" ];

                                let out = [...arr].map(e => Object.keys(e).map(k => !filter.includes(k) ? delete e[k] :true) && e);
                                console.log(out)








                                share|improve this answer



























                                  1

















                                  let arr = [
                                  prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                                  prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                                  prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                                  ]

                                  let filter = [ "prop1", "prop2" ];

                                  let out = [...arr].map(e => Object.keys(e).map(k => !filter.includes(k) ? delete e[k] :true) && e);
                                  console.log(out)








                                  share|improve this answer

























                                    1












                                    1








                                    1










                                    let arr = [
                                    prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                                    prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                                    prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                                    ]

                                    let filter = [ "prop1", "prop2" ];

                                    let out = [...arr].map(e => Object.keys(e).map(k => !filter.includes(k) ? delete e[k] :true) && e);
                                    console.log(out)








                                    share|improve this answer
















                                    let arr = [
                                    prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                                    prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                                    prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                                    ]

                                    let filter = [ "prop1", "prop2" ];

                                    let out = [...arr].map(e => Object.keys(e).map(k => !filter.includes(k) ? delete e[k] :true) && e);
                                    console.log(out)








                                    let arr = [
                                    prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                                    prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                                    prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                                    ]

                                    let filter = [ "prop1", "prop2" ];

                                    let out = [...arr].map(e => Object.keys(e).map(k => !filter.includes(k) ? delete e[k] :true) && e);
                                    console.log(out)





                                    let arr = [
                                    prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1",
                                    prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2",
                                    prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"
                                    ]

                                    let filter = [ "prop1", "prop2" ];

                                    let out = [...arr].map(e => Object.keys(e).map(k => !filter.includes(k) ? delete e[k] :true) && e);
                                    console.log(out)






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 22 at 11:54









                                    AZ_AZ_

                                    942310




                                    942310



























                                        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%2f55298528%2fget-object-properties-by-predefined-property-names-array%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                        Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                        Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript