Length of items by field in an array

Darren

How can I get the number of objects that contain a certain value from an array?

Using componentDidMount to get stored data in a Firestore db as

componentDidMount() {
 db.collection(`company/${this.props.userID}/campaigns`).onSnapshot(collection => {
  const campaigns = collection.docs.map(doc => doc.data());
  this.setState({ 
    campaigns,
   });
 });
}

This mounts the data in this.state but I would like to get the count value for each by their status field. Here is an example below whereas I am trying to get the length of entries for status = active draft paused.

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      campaigns: [{
          id: 1,
          status: "draft",
        },
        {
          id: 2,
          status: "active",
        },
        {
          id: 3,
          status: "draft",
        },
        {
          id: 4,
          status: "paused",
        }
      ]
    };
  }

  render() {
    const totalCampaigns = this.state.campaigns
    const activeCampaigns = this.state.campaigns.filter(campaign => campaign.status === "active")
    const draftCampaigns = this.state.campaigns.filter(campaign => campaign.status === "draft")
    const pausedCampaigns = this.state.campaigns.filter(campaign => campaign.status === "paused")
    return (
      <div>
        total {totalCampaigns.length}
        Active {activeCampaigns.length}
        Drafts {draftCampaigns.length}
        Paused {pausedCampaigns.length}
      </div>
    );
  }
}

ReactDOM.render( < App / > , document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root"></div>

Could you please help me understand what I am doing wrong or if there is a better method for this?

brk

Use reduce method on state.campaigns and return an object where the keys will be the status and the value will be the number of occurrence of the status

let state = {
  campaigns: [{
      id: 1,
      status: "draft",
    },
    {
      id: 2,
      status: "active",
    },
    {
      id: 3,
      status: "draft",
    },
    {
      id: 4,
      status: "paused",
    }
  ]
};

let getCount = state.campaigns.reduce(function(acc, curr) {
  if (!acc.hasOwnProperty(curr.status)) {
    acc[curr.status] = 1
  } else {
    acc[curr.status] += 1

  }

  return acc;


}, {})
console.log(getCount)

render() {
    const totalCampaigns =  // here goes the the getCount
    return (
      <div>
         Active {getCount.draft}
        Drafts {getCount.active}
        Paused {getCount.paused}
      </div>
    );
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Flexform field select length of items

From Dev

Group items by field in array

From Dev

Getting array length of items with a specific attribute

From Dev

increment document field depending on array length

From Dev

SqlAlchemy: Querying the length json field having an array

From Dev

How to sort documents based on length of an Array field

From Dev

Sum all of the items in a JSONB array field

From Dev

Why jq array returns length is greater than actual no of array items

From Dev

String array length printed instead of the Number of Array Items in C++

From Dev

Why jq array returns length is greater than actual no of array items

From Dev

Populate limited number of items in array, but keep array length - mongoose

From Dev

How to create array of predefined length and then add items to it in Angularjs

From Dev

array in R number of items to replace is not a multiple of replacement length

From Dev

How to remove items with certain length from array in php

From Dev

javascript - multidimensional array contains 4 items but length indicates: 0

From Dev

Is using own int capacity faster than using .length field of an array?

From Dev

Find length of each array field within a JSON object using jq

From Dev

Java - How to check the array length via reflection (Field in class)

From Dev

How to publish a mongodb array length as an additional collection field?

From Dev

How do you set a length and justify an Array field in Perl?

From Dev

How to order documents by array field length in firebase firestore

From Dev

Sort items in an array by more than one field with lodash

From Dev

Mongodb query by array field and show queried items in result

From Dev

Count child array items when field is equal to true

From Dev

Mongoose get count of items in array field across all docs

From Dev

Meteor: how to automatically populate field with length of array stored in other field in a collection?

From Dev

Meteor: how to automatically populate field with length of array stored in other field in a collection?

From Dev

Increasing Binary field length

From Dev

length field in Object Class

Related Related

  1. 1

    Flexform field select length of items

  2. 2

    Group items by field in array

  3. 3

    Getting array length of items with a specific attribute

  4. 4

    increment document field depending on array length

  5. 5

    SqlAlchemy: Querying the length json field having an array

  6. 6

    How to sort documents based on length of an Array field

  7. 7

    Sum all of the items in a JSONB array field

  8. 8

    Why jq array returns length is greater than actual no of array items

  9. 9

    String array length printed instead of the Number of Array Items in C++

  10. 10

    Why jq array returns length is greater than actual no of array items

  11. 11

    Populate limited number of items in array, but keep array length - mongoose

  12. 12

    How to create array of predefined length and then add items to it in Angularjs

  13. 13

    array in R number of items to replace is not a multiple of replacement length

  14. 14

    How to remove items with certain length from array in php

  15. 15

    javascript - multidimensional array contains 4 items but length indicates: 0

  16. 16

    Is using own int capacity faster than using .length field of an array?

  17. 17

    Find length of each array field within a JSON object using jq

  18. 18

    Java - How to check the array length via reflection (Field in class)

  19. 19

    How to publish a mongodb array length as an additional collection field?

  20. 20

    How do you set a length and justify an Array field in Perl?

  21. 21

    How to order documents by array field length in firebase firestore

  22. 22

    Sort items in an array by more than one field with lodash

  23. 23

    Mongodb query by array field and show queried items in result

  24. 24

    Count child array items when field is equal to true

  25. 25

    Mongoose get count of items in array field across all docs

  26. 26

    Meteor: how to automatically populate field with length of array stored in other field in a collection?

  27. 27

    Meteor: how to automatically populate field with length of array stored in other field in a collection?

  28. 28

    Increasing Binary field length

  29. 29

    length field in Object Class

HotTag

Archive