How to loop through an object array and output the array with a specific object property javascript












0















var obo = [{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}
]


I want to loop through and return the array. i have tried splice its not working



var obo = [{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
}
]


Am trying to populate the array so as not to have parcelid in it










share|improve this question




















  • 1





    What is your specific property, which property in the array are you looking for?

    – squeekyDave
    Nov 15 '18 at 16:22
















0















var obo = [{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}
]


I want to loop through and return the array. i have tried splice its not working



var obo = [{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
}
]


Am trying to populate the array so as not to have parcelid in it










share|improve this question




















  • 1





    What is your specific property, which property in the array are you looking for?

    – squeekyDave
    Nov 15 '18 at 16:22














0












0








0


0






var obo = [{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}
]


I want to loop through and return the array. i have tried splice its not working



var obo = [{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
}
]


Am trying to populate the array so as not to have parcelid in it










share|improve this question
















var obo = [{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}
]


I want to loop through and return the array. i have tried splice its not working



var obo = [{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
}
]


Am trying to populate the array so as not to have parcelid in it







javascript arrays object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:58









Mohammad

15.8k123664




15.8k123664










asked Nov 15 '18 at 16:16









OpenwellOpenwell

12




12








  • 1





    What is your specific property, which property in the array are you looking for?

    – squeekyDave
    Nov 15 '18 at 16:22














  • 1





    What is your specific property, which property in the array are you looking for?

    – squeekyDave
    Nov 15 '18 at 16:22








1




1





What is your specific property, which property in the array are you looking for?

– squeekyDave
Nov 15 '18 at 16:22





What is your specific property, which property in the array are you looking for?

– squeekyDave
Nov 15 '18 at 16:22












4 Answers
4






active

oldest

votes


















4














You can use .map() with some object destructuring:






var data = [
{"parcelId": "009", "userid": "tomi", "location": "kwara"},
{"parcelId": "009", "userid": "tomi", "location": "kwara"},
{"parcelId": "009", "userid": "tomi", "location": "kwara"}
];

var result = data.map(({userid, location, ...rest}) => ({userid, location}));

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }





References:




  • Array.prototype.map()

  • Object Destructuring






share|improve this answer

































    0














    You need to use .map()



    obo = obo.map(function(item){
    return {
    userid: item.userid,
    location: item.location
    };
    });





    var obo = [
    {
    "parcelId": "009",
    "userid": "tomi",
    "location": "kwara"
    },
    {
    "parcelId": "009",
    "userid": "tomi",
    "location": "kwara"
    },
    {
    "parcelId": "009",
    "userid": "tomi",
    "location": "kwara"
    }
    ];

    obo = obo.map(function(item){
    return {
    userid: item.userid,
    location: item.location
    };
    });
    console.log(obo);








    share|improve this answer


























    • Since delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach

      – charlietfl
      Nov 15 '18 at 16:21





















    0














    How about that with Array.prototype.map()?






    var obo = [{
    "parcelId": "009",
    "userid": "tomi",
    "location": "kwara"
    },
    {
    "parcelId": "009",
    "userid": "tomi",
    "location": "kwara"
    },
    {
    "parcelId": "009",
    "userid": "tomi",
    "location": "kwara"
    }
    ]

    var expected = ;

    obo.map((elm, ind) => {
    expected.push({
    "userid": elm.userid,
    "location": elm.location
    })
    })
    console.log(expected)








    share|improve this answer































      0














      You can try something like this



      obo = obo.map(el => {
      let obj = {
      userid: el.userid,
      location: el.location
      };
      return obj;
      });


      or for a one liner, you can do this



      obo = obo.map(({userid,location})=>({userid,location})));






      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%2f53323641%2fhow-to-loop-through-an-object-array-and-output-the-array-with-a-specific-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









        4














        You can use .map() with some object destructuring:






        var data = [
        {"parcelId": "009", "userid": "tomi", "location": "kwara"},
        {"parcelId": "009", "userid": "tomi", "location": "kwara"},
        {"parcelId": "009", "userid": "tomi", "location": "kwara"}
        ];

        var result = data.map(({userid, location, ...rest}) => ({userid, location}));

        console.log(result);

        .as-console-wrapper { max-height: 100% !important; top: 0; }





        References:




        • Array.prototype.map()

        • Object Destructuring






        share|improve this answer






























          4














          You can use .map() with some object destructuring:






          var data = [
          {"parcelId": "009", "userid": "tomi", "location": "kwara"},
          {"parcelId": "009", "userid": "tomi", "location": "kwara"},
          {"parcelId": "009", "userid": "tomi", "location": "kwara"}
          ];

          var result = data.map(({userid, location, ...rest}) => ({userid, location}));

          console.log(result);

          .as-console-wrapper { max-height: 100% !important; top: 0; }





          References:




          • Array.prototype.map()

          • Object Destructuring






          share|improve this answer




























            4












            4








            4







            You can use .map() with some object destructuring:






            var data = [
            {"parcelId": "009", "userid": "tomi", "location": "kwara"},
            {"parcelId": "009", "userid": "tomi", "location": "kwara"},
            {"parcelId": "009", "userid": "tomi", "location": "kwara"}
            ];

            var result = data.map(({userid, location, ...rest}) => ({userid, location}));

            console.log(result);

            .as-console-wrapper { max-height: 100% !important; top: 0; }





            References:




            • Array.prototype.map()

            • Object Destructuring






            share|improve this answer















            You can use .map() with some object destructuring:






            var data = [
            {"parcelId": "009", "userid": "tomi", "location": "kwara"},
            {"parcelId": "009", "userid": "tomi", "location": "kwara"},
            {"parcelId": "009", "userid": "tomi", "location": "kwara"}
            ];

            var result = data.map(({userid, location, ...rest}) => ({userid, location}));

            console.log(result);

            .as-console-wrapper { max-height: 100% !important; top: 0; }





            References:




            • Array.prototype.map()

            • Object Destructuring






            var data = [
            {"parcelId": "009", "userid": "tomi", "location": "kwara"},
            {"parcelId": "009", "userid": "tomi", "location": "kwara"},
            {"parcelId": "009", "userid": "tomi", "location": "kwara"}
            ];

            var result = data.map(({userid, location, ...rest}) => ({userid, location}));

            console.log(result);

            .as-console-wrapper { max-height: 100% !important; top: 0; }





            var data = [
            {"parcelId": "009", "userid": "tomi", "location": "kwara"},
            {"parcelId": "009", "userid": "tomi", "location": "kwara"},
            {"parcelId": "009", "userid": "tomi", "location": "kwara"}
            ];

            var result = data.map(({userid, location, ...rest}) => ({userid, location}));

            console.log(result);

            .as-console-wrapper { max-height: 100% !important; top: 0; }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 16:25

























            answered Nov 15 '18 at 16:20









            Mohammad UsmanMohammad Usman

            21.2k124558




            21.2k124558

























                0














                You need to use .map()



                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });





                var obo = [
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                }
                ];

                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });
                console.log(obo);








                share|improve this answer


























                • Since delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach

                  – charlietfl
                  Nov 15 '18 at 16:21


















                0














                You need to use .map()



                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });





                var obo = [
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                }
                ];

                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });
                console.log(obo);








                share|improve this answer


























                • Since delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach

                  – charlietfl
                  Nov 15 '18 at 16:21
















                0












                0








                0







                You need to use .map()



                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });





                var obo = [
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                }
                ];

                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });
                console.log(obo);








                share|improve this answer















                You need to use .map()



                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });





                var obo = [
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                }
                ];

                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });
                console.log(obo);








                var obo = [
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                }
                ];

                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });
                console.log(obo);





                var obo = [
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                }
                ];

                obo = obo.map(function(item){
                return {
                userid: item.userid,
                location: item.location
                };
                });
                console.log(obo);






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 16:21

























                answered Nov 15 '18 at 16:20









                MohammadMohammad

                15.8k123664




                15.8k123664













                • Since delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach

                  – charlietfl
                  Nov 15 '18 at 16:21





















                • Since delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach

                  – charlietfl
                  Nov 15 '18 at 16:21



















                Since delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach

                – charlietfl
                Nov 15 '18 at 16:21







                Since delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach

                – charlietfl
                Nov 15 '18 at 16:21













                0














                How about that with Array.prototype.map()?






                var obo = [{
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                },
                {
                "parcelId": "009",
                "userid": "tomi",
                "location": "kwara"
                }
                ]

                var expected = ;

                obo.map((elm, ind) => {
                expected.push({
                "userid": elm.userid,
                "location": elm.location
                })
                })
                console.log(expected)








                share|improve this answer




























                  0














                  How about that with Array.prototype.map()?






                  var obo = [{
                  "parcelId": "009",
                  "userid": "tomi",
                  "location": "kwara"
                  },
                  {
                  "parcelId": "009",
                  "userid": "tomi",
                  "location": "kwara"
                  },
                  {
                  "parcelId": "009",
                  "userid": "tomi",
                  "location": "kwara"
                  }
                  ]

                  var expected = ;

                  obo.map((elm, ind) => {
                  expected.push({
                  "userid": elm.userid,
                  "location": elm.location
                  })
                  })
                  console.log(expected)








                  share|improve this answer


























                    0












                    0








                    0







                    How about that with Array.prototype.map()?






                    var obo = [{
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    },
                    {
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    },
                    {
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    }
                    ]

                    var expected = ;

                    obo.map((elm, ind) => {
                    expected.push({
                    "userid": elm.userid,
                    "location": elm.location
                    })
                    })
                    console.log(expected)








                    share|improve this answer













                    How about that with Array.prototype.map()?






                    var obo = [{
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    },
                    {
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    },
                    {
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    }
                    ]

                    var expected = ;

                    obo.map((elm, ind) => {
                    expected.push({
                    "userid": elm.userid,
                    "location": elm.location
                    })
                    })
                    console.log(expected)








                    var obo = [{
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    },
                    {
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    },
                    {
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    }
                    ]

                    var expected = ;

                    obo.map((elm, ind) => {
                    expected.push({
                    "userid": elm.userid,
                    "location": elm.location
                    })
                    })
                    console.log(expected)





                    var obo = [{
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    },
                    {
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    },
                    {
                    "parcelId": "009",
                    "userid": "tomi",
                    "location": "kwara"
                    }
                    ]

                    var expected = ;

                    obo.map((elm, ind) => {
                    expected.push({
                    "userid": elm.userid,
                    "location": elm.location
                    })
                    })
                    console.log(expected)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 16:23









                    Always SunnyAlways Sunny

                    16.7k32848




                    16.7k32848























                        0














                        You can try something like this



                        obo = obo.map(el => {
                        let obj = {
                        userid: el.userid,
                        location: el.location
                        };
                        return obj;
                        });


                        or for a one liner, you can do this



                        obo = obo.map(({userid,location})=>({userid,location})));






                        share|improve this answer




























                          0














                          You can try something like this



                          obo = obo.map(el => {
                          let obj = {
                          userid: el.userid,
                          location: el.location
                          };
                          return obj;
                          });


                          or for a one liner, you can do this



                          obo = obo.map(({userid,location})=>({userid,location})));






                          share|improve this answer


























                            0












                            0








                            0







                            You can try something like this



                            obo = obo.map(el => {
                            let obj = {
                            userid: el.userid,
                            location: el.location
                            };
                            return obj;
                            });


                            or for a one liner, you can do this



                            obo = obo.map(({userid,location})=>({userid,location})));






                            share|improve this answer













                            You can try something like this



                            obo = obo.map(el => {
                            let obj = {
                            userid: el.userid,
                            location: el.location
                            };
                            return obj;
                            });


                            or for a one liner, you can do this



                            obo = obo.map(({userid,location})=>({userid,location})));







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 16:24









                            theapologisttheapologist

                            576215




                            576215






























                                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%2f53323641%2fhow-to-loop-through-an-object-array-and-output-the-array-with-a-specific-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

                                List item for chat from Array inside array React Native

                                Thiostrepton

                                Caerphilly