How to find if object inside array is NOT empty?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?How do I test for an empty JavaScript object?Sort array of objects by string property valueHow do I empty an array in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

Fencing style for blades that can attack from a distance

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

How can I make my BBEG immortal short of making them a Lich or Vampire?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

What defenses are there against being summoned by the Gate spell?

What are the differences between the usage of 'it' and 'they'?

In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?

Writing rule stating superpower from different root cause is bad writing

What does it mean : "Canonical representative of Sbox is 0123468A5BCF79DE"? and How can we calculate this representative for Sbox?

Why do falling prices hurt debtors?

Theorems that impeded progress

Test whether all array elements are factors of a number

How to write a macro that is braces sensitive?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Languages that we cannot (dis)prove to be Context-Free

What are these boxed doors outside store fronts in New York?

Dragon forelimb placement

Is it important to consider tone, melody, and musical form while writing a song?

LaTeX closing $ signs makes cursor jump

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

What does it mean to describe someone as a butt steak?



How to find if object inside array is NOT empty?


How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?How do I test for an empty JavaScript object?Sort array of objects by string property valueHow do I empty an array in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with 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















Say I have an array, like so:



arr = [, , , ]


I want to be able to know the length of the array, only counting objects within in it that have at least one property.



[, name: "Derby County", odds: 2, , ] // 1

[, name: "Derby County", odds: 2, name: "Fullham", odds: 3 ,] // 2


How do I achieve this?










share|improve this question
























  • Object.keys(obj).length will yield a non-zero result when there's at least one iterable, non-inherited property.

    – ziggy wiggy
    Mar 9 at 2:27











  • detect when something is added this has more than one way to interpret it, with quite different solutions

    – André Werlang
    Mar 9 at 2:41











  • You're right @AndréWerlang I'll update OP now, thank you

    – A7DC
    Mar 9 at 2:52











  • I've updated the OP, hopefully that's clearer?

    – A7DC
    Mar 9 at 2:56

















0















Say I have an array, like so:



arr = [, , , ]


I want to be able to know the length of the array, only counting objects within in it that have at least one property.



[, name: "Derby County", odds: 2, , ] // 1

[, name: "Derby County", odds: 2, name: "Fullham", odds: 3 ,] // 2


How do I achieve this?










share|improve this question
























  • Object.keys(obj).length will yield a non-zero result when there's at least one iterable, non-inherited property.

    – ziggy wiggy
    Mar 9 at 2:27











  • detect when something is added this has more than one way to interpret it, with quite different solutions

    – André Werlang
    Mar 9 at 2:41











  • You're right @AndréWerlang I'll update OP now, thank you

    – A7DC
    Mar 9 at 2:52











  • I've updated the OP, hopefully that's clearer?

    – A7DC
    Mar 9 at 2:56













0












0








0








Say I have an array, like so:



arr = [, , , ]


I want to be able to know the length of the array, only counting objects within in it that have at least one property.



[, name: "Derby County", odds: 2, , ] // 1

[, name: "Derby County", odds: 2, name: "Fullham", odds: 3 ,] // 2


How do I achieve this?










share|improve this question
















Say I have an array, like so:



arr = [, , , ]


I want to be able to know the length of the array, only counting objects within in it that have at least one property.



[, name: "Derby County", odds: 2, , ] // 1

[, name: "Derby County", odds: 2, name: "Fullham", odds: 3 ,] // 2


How do I achieve this?







javascript arrays json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 3:22







A7DC

















asked Mar 9 at 2:21









A7DCA7DC

5081621




5081621












  • Object.keys(obj).length will yield a non-zero result when there's at least one iterable, non-inherited property.

    – ziggy wiggy
    Mar 9 at 2:27











  • detect when something is added this has more than one way to interpret it, with quite different solutions

    – André Werlang
    Mar 9 at 2:41











  • You're right @AndréWerlang I'll update OP now, thank you

    – A7DC
    Mar 9 at 2:52











  • I've updated the OP, hopefully that's clearer?

    – A7DC
    Mar 9 at 2:56

















  • Object.keys(obj).length will yield a non-zero result when there's at least one iterable, non-inherited property.

    – ziggy wiggy
    Mar 9 at 2:27











  • detect when something is added this has more than one way to interpret it, with quite different solutions

    – André Werlang
    Mar 9 at 2:41











  • You're right @AndréWerlang I'll update OP now, thank you

    – A7DC
    Mar 9 at 2:52











  • I've updated the OP, hopefully that's clearer?

    – A7DC
    Mar 9 at 2:56
















Object.keys(obj).length will yield a non-zero result when there's at least one iterable, non-inherited property.

– ziggy wiggy
Mar 9 at 2:27





Object.keys(obj).length will yield a non-zero result when there's at least one iterable, non-inherited property.

– ziggy wiggy
Mar 9 at 2:27













detect when something is added this has more than one way to interpret it, with quite different solutions

– André Werlang
Mar 9 at 2:41





detect when something is added this has more than one way to interpret it, with quite different solutions

– André Werlang
Mar 9 at 2:41













You're right @AndréWerlang I'll update OP now, thank you

– A7DC
Mar 9 at 2:52





You're right @AndréWerlang I'll update OP now, thank you

– A7DC
Mar 9 at 2:52













I've updated the OP, hopefully that's clearer?

– A7DC
Mar 9 at 2:56





I've updated the OP, hopefully that's clearer?

– A7DC
Mar 9 at 2:56












2 Answers
2






active

oldest

votes


















1














arr.filter(x => Object.keys(x).length).length


As explained in other answers, Object.keys() returns property names from a given object. The inner .length is a shortcut to filter only items that have at least one property. The outer .length tells how many objects fit the description.



UPDATE:



The [].filter() method takes a function that returns a thruthy/falsy value. A number greater than 0 is thruthy, so it's the same as .length !== 0.



The assumption here is that any element contained in the array is non-null. Under this assumption it makes no sense checking the object for null inside the [].filter(). When using TypeScript, it's a static check for arr. If the assumption is broken, then an error is thrown, which it's something I usually desire. I don't hide runtime errors. If there's a runtime error here, I'll review the assumption. Yet I'm not sure it's the case here.






share|improve this answer




















  • 1





    Remember to check for null arr.filter(x => x ? Object.keys(x).length > 0 : false).length;

    – Thilina Hasantha
    Mar 9 at 3:19












  • Thanks for the reply and sorry, I've updated the OP again! Hopefully now I've articulated myself properly

    – A7DC
    Mar 9 at 3:23






  • 1





    @A7DC the edit doesn't change the answer, it still complies, I guess the .length access made you think it would accumulate properties?

    – André Werlang
    Mar 9 at 12:16






  • 1





    @ThilinaHasantha it's a valid assumption to expect the input array contains only non-null objects. Besides, it can be also desired it throws an error if that assumption fails

    – André Werlang
    Mar 9 at 12:19


















1














If you do:



arr.map(x=> Object.keys(x).length)


you'll get:



[ 0, 0, 0, 1 ]


If the object is empty then there are no keys and so its length is 0.



If you need a true/false result do:



arr.map(x=> Object.keys(x).length).some(x=>x>0)


Examples:






console.log("[,]", [,].map(x=> Object.keys(x).length).some(x=>x>0))

console.log("[,a: 1]", [,a: 1].map(x=> Object.keys(x).length).some(x=>x>0))








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%2f55073388%2fhow-to-find-if-object-inside-array-is-not-empty%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    arr.filter(x => Object.keys(x).length).length


    As explained in other answers, Object.keys() returns property names from a given object. The inner .length is a shortcut to filter only items that have at least one property. The outer .length tells how many objects fit the description.



    UPDATE:



    The [].filter() method takes a function that returns a thruthy/falsy value. A number greater than 0 is thruthy, so it's the same as .length !== 0.



    The assumption here is that any element contained in the array is non-null. Under this assumption it makes no sense checking the object for null inside the [].filter(). When using TypeScript, it's a static check for arr. If the assumption is broken, then an error is thrown, which it's something I usually desire. I don't hide runtime errors. If there's a runtime error here, I'll review the assumption. Yet I'm not sure it's the case here.






    share|improve this answer




















    • 1





      Remember to check for null arr.filter(x => x ? Object.keys(x).length > 0 : false).length;

      – Thilina Hasantha
      Mar 9 at 3:19












    • Thanks for the reply and sorry, I've updated the OP again! Hopefully now I've articulated myself properly

      – A7DC
      Mar 9 at 3:23






    • 1





      @A7DC the edit doesn't change the answer, it still complies, I guess the .length access made you think it would accumulate properties?

      – André Werlang
      Mar 9 at 12:16






    • 1





      @ThilinaHasantha it's a valid assumption to expect the input array contains only non-null objects. Besides, it can be also desired it throws an error if that assumption fails

      – André Werlang
      Mar 9 at 12:19















    1














    arr.filter(x => Object.keys(x).length).length


    As explained in other answers, Object.keys() returns property names from a given object. The inner .length is a shortcut to filter only items that have at least one property. The outer .length tells how many objects fit the description.



    UPDATE:



    The [].filter() method takes a function that returns a thruthy/falsy value. A number greater than 0 is thruthy, so it's the same as .length !== 0.



    The assumption here is that any element contained in the array is non-null. Under this assumption it makes no sense checking the object for null inside the [].filter(). When using TypeScript, it's a static check for arr. If the assumption is broken, then an error is thrown, which it's something I usually desire. I don't hide runtime errors. If there's a runtime error here, I'll review the assumption. Yet I'm not sure it's the case here.






    share|improve this answer




















    • 1





      Remember to check for null arr.filter(x => x ? Object.keys(x).length > 0 : false).length;

      – Thilina Hasantha
      Mar 9 at 3:19












    • Thanks for the reply and sorry, I've updated the OP again! Hopefully now I've articulated myself properly

      – A7DC
      Mar 9 at 3:23






    • 1





      @A7DC the edit doesn't change the answer, it still complies, I guess the .length access made you think it would accumulate properties?

      – André Werlang
      Mar 9 at 12:16






    • 1





      @ThilinaHasantha it's a valid assumption to expect the input array contains only non-null objects. Besides, it can be also desired it throws an error if that assumption fails

      – André Werlang
      Mar 9 at 12:19













    1












    1








    1







    arr.filter(x => Object.keys(x).length).length


    As explained in other answers, Object.keys() returns property names from a given object. The inner .length is a shortcut to filter only items that have at least one property. The outer .length tells how many objects fit the description.



    UPDATE:



    The [].filter() method takes a function that returns a thruthy/falsy value. A number greater than 0 is thruthy, so it's the same as .length !== 0.



    The assumption here is that any element contained in the array is non-null. Under this assumption it makes no sense checking the object for null inside the [].filter(). When using TypeScript, it's a static check for arr. If the assumption is broken, then an error is thrown, which it's something I usually desire. I don't hide runtime errors. If there's a runtime error here, I'll review the assumption. Yet I'm not sure it's the case here.






    share|improve this answer















    arr.filter(x => Object.keys(x).length).length


    As explained in other answers, Object.keys() returns property names from a given object. The inner .length is a shortcut to filter only items that have at least one property. The outer .length tells how many objects fit the description.



    UPDATE:



    The [].filter() method takes a function that returns a thruthy/falsy value. A number greater than 0 is thruthy, so it's the same as .length !== 0.



    The assumption here is that any element contained in the array is non-null. Under this assumption it makes no sense checking the object for null inside the [].filter(). When using TypeScript, it's a static check for arr. If the assumption is broken, then an error is thrown, which it's something I usually desire. I don't hide runtime errors. If there's a runtime error here, I'll review the assumption. Yet I'm not sure it's the case here.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 9 at 12:30

























    answered Mar 9 at 3:01









    André WerlangAndré Werlang

    4,1622141




    4,1622141







    • 1





      Remember to check for null arr.filter(x => x ? Object.keys(x).length > 0 : false).length;

      – Thilina Hasantha
      Mar 9 at 3:19












    • Thanks for the reply and sorry, I've updated the OP again! Hopefully now I've articulated myself properly

      – A7DC
      Mar 9 at 3:23






    • 1





      @A7DC the edit doesn't change the answer, it still complies, I guess the .length access made you think it would accumulate properties?

      – André Werlang
      Mar 9 at 12:16






    • 1





      @ThilinaHasantha it's a valid assumption to expect the input array contains only non-null objects. Besides, it can be also desired it throws an error if that assumption fails

      – André Werlang
      Mar 9 at 12:19












    • 1





      Remember to check for null arr.filter(x => x ? Object.keys(x).length > 0 : false).length;

      – Thilina Hasantha
      Mar 9 at 3:19












    • Thanks for the reply and sorry, I've updated the OP again! Hopefully now I've articulated myself properly

      – A7DC
      Mar 9 at 3:23






    • 1





      @A7DC the edit doesn't change the answer, it still complies, I guess the .length access made you think it would accumulate properties?

      – André Werlang
      Mar 9 at 12:16






    • 1





      @ThilinaHasantha it's a valid assumption to expect the input array contains only non-null objects. Besides, it can be also desired it throws an error if that assumption fails

      – André Werlang
      Mar 9 at 12:19







    1




    1





    Remember to check for null arr.filter(x => x ? Object.keys(x).length > 0 : false).length;

    – Thilina Hasantha
    Mar 9 at 3:19






    Remember to check for null arr.filter(x => x ? Object.keys(x).length > 0 : false).length;

    – Thilina Hasantha
    Mar 9 at 3:19














    Thanks for the reply and sorry, I've updated the OP again! Hopefully now I've articulated myself properly

    – A7DC
    Mar 9 at 3:23





    Thanks for the reply and sorry, I've updated the OP again! Hopefully now I've articulated myself properly

    – A7DC
    Mar 9 at 3:23




    1




    1





    @A7DC the edit doesn't change the answer, it still complies, I guess the .length access made you think it would accumulate properties?

    – André Werlang
    Mar 9 at 12:16





    @A7DC the edit doesn't change the answer, it still complies, I guess the .length access made you think it would accumulate properties?

    – André Werlang
    Mar 9 at 12:16




    1




    1





    @ThilinaHasantha it's a valid assumption to expect the input array contains only non-null objects. Besides, it can be also desired it throws an error if that assumption fails

    – André Werlang
    Mar 9 at 12:19





    @ThilinaHasantha it's a valid assumption to expect the input array contains only non-null objects. Besides, it can be also desired it throws an error if that assumption fails

    – André Werlang
    Mar 9 at 12:19













    1














    If you do:



    arr.map(x=> Object.keys(x).length)


    you'll get:



    [ 0, 0, 0, 1 ]


    If the object is empty then there are no keys and so its length is 0.



    If you need a true/false result do:



    arr.map(x=> Object.keys(x).length).some(x=>x>0)


    Examples:






    console.log("[,]", [,].map(x=> Object.keys(x).length).some(x=>x>0))

    console.log("[,a: 1]", [,a: 1].map(x=> Object.keys(x).length).some(x=>x>0))








    share|improve this answer





























      1














      If you do:



      arr.map(x=> Object.keys(x).length)


      you'll get:



      [ 0, 0, 0, 1 ]


      If the object is empty then there are no keys and so its length is 0.



      If you need a true/false result do:



      arr.map(x=> Object.keys(x).length).some(x=>x>0)


      Examples:






      console.log("[,]", [,].map(x=> Object.keys(x).length).some(x=>x>0))

      console.log("[,a: 1]", [,a: 1].map(x=> Object.keys(x).length).some(x=>x>0))








      share|improve this answer



























        1












        1








        1







        If you do:



        arr.map(x=> Object.keys(x).length)


        you'll get:



        [ 0, 0, 0, 1 ]


        If the object is empty then there are no keys and so its length is 0.



        If you need a true/false result do:



        arr.map(x=> Object.keys(x).length).some(x=>x>0)


        Examples:






        console.log("[,]", [,].map(x=> Object.keys(x).length).some(x=>x>0))

        console.log("[,a: 1]", [,a: 1].map(x=> Object.keys(x).length).some(x=>x>0))








        share|improve this answer















        If you do:



        arr.map(x=> Object.keys(x).length)


        you'll get:



        [ 0, 0, 0, 1 ]


        If the object is empty then there are no keys and so its length is 0.



        If you need a true/false result do:



        arr.map(x=> Object.keys(x).length).some(x=>x>0)


        Examples:






        console.log("[,]", [,].map(x=> Object.keys(x).length).some(x=>x>0))

        console.log("[,a: 1]", [,a: 1].map(x=> Object.keys(x).length).some(x=>x>0))








        console.log("[,]", [,].map(x=> Object.keys(x).length).some(x=>x>0))

        console.log("[,a: 1]", [,a: 1].map(x=> Object.keys(x).length).some(x=>x>0))





        console.log("[,]", [,].map(x=> Object.keys(x).length).some(x=>x>0))

        console.log("[,a: 1]", [,a: 1].map(x=> Object.keys(x).length).some(x=>x>0))






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 9 at 2:33

























        answered Mar 9 at 2:28









        R. SchifiniR. Schifini

        6,84821928




        6,84821928



























            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%2f55073388%2fhow-to-find-if-object-inside-array-is-not-empty%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

            How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229