0

I have an array of objects, each element looking something like this:

campaigns: (5) ["get.dentalintel.net/call-insight-DeshaunWatson", "get.dentalintel.net/practice-analysis-DeshaunWatson", "get.dentalintel.net/morning-huddle-DeshaunWatson", "get.dentalintel.net/new-morning-huddle-DeshaunWatson", "get.dentalintel.net/followups-DeshaunWatson"]
email: "[email protected]"
name: "Deshaun Watson"
submissions: [{…}]
user_id: 166

within each element there is an array of submission objects that appear like this:

campaigns: (5) ["get.dentalintel.net/call-insight-DeshaunWatson", "get.dentalintel.net/practice-analysis-DeshaunWatson", "get.dentalintel.net/morning-huddle-DeshaunWatson", "get.dentalintel.net/new-morning-huddle-DeshaunWatson", "get.dentalintel.net/followups-DeshaunWatson"]
email: "[email protected]"
name: "Deshaun Watson"
submissions: Array(1)
0:
addedAt: 1574185321138
canonical-vid: 13476551
form-submissions: []
identity-profiles: [{…}]
is-contact: true
merge-audits: []
merged-vids: []
portal-id: 2271480
profile-token: "AO_T-mOo4xVvqmFyVkiizX8l0pCPZTakJOfo02uTm2kWzr68fzpXI6-xmyh4Gmj_Pzpp8IBDdbEN9CCRW4GeMfybZaSBiMZ8xXo2U2dylZ7QD3CufR-ERrazbZlKPaDyVzxaCqwvXU3W"
profile-url: "https://app.hubspot.com/contacts/2271480/contact/13476551"
properties: {partner_email: {…}, utm_campaign: {…}, lastmodifieddate: {…}, partner_last_name: {…}, email: {…}}
vid: 13476551
__proto__: Object
length: 1
__proto__: Array(0)
user_id: 166

The only property of note on this is the addedAt value. I want to filter out submission objects based on a date range. This is what I have tried so far with no luck:


  filterByDate = () => {
    const { partners, endDate, startDate } = this.state;
    partners.forEach(partner => {
      for (let i = 0; i < partner.submissions.length; i++) {
        if (
          moment(partner.submissions[i].addedAt).isBetween(startDate, endDate)
        ) {
          return partners;
        } else {
          return partner.submissions.splice(i, 1);
        }
      }
    });
  };

It removes some of the submission objects, but never the correct ones. Any suggestions on a better approach would be appreciated.

1

2 Answers 2

1

For filtering use Array.filter. This returns a new array with only the values that meet the boolean returned from the callback. For creating a new array from existing arrays use Array.map instead of Array.forEach. forEach loop only iterates over the values but Array.map returns a modified or new value for every element.

const filterByDate = () => {
    const { partners, endDate, startDate } = this.state;
    const filteredPartners = partners.map(partner => {
        return {
            ...partner,
            submissions: partner.submissions.filter(submission =>
                moment(submission.addedAt).isBetween(startDate, endDate)
            )
        };
    });
};

Example Array.filter and Array.map (No mutation)

const testArray = [{
  id: 1
}, {
  id: 2
}, {
  id: 3
}, {
  id: 4
}]
const filtered = testArray.filter(e => e.id !== 3)
const mapped = testArray.map(e => {
  return {
    id: e.id,
    name: `Name of ${e.id}`
  }
})

console.log("Test Array: ", testArray)
console.log("Filtered Array: ", filtered)
console.log("Mapped Array: ", mapped)

Sign up to request clarification or add additional context in comments.

1 Comment

This is great. Thank you so much for the explanation. It works perfectly.
0

You can use Array.prototype.filter() for this use case

filterByDate = () => {
  const { partners, endDate, startDate } = this.state;
  partners.forEach(partner => {
    partner.submissions = partner.submissions.filter(sub =>  moment(partner.submissions[i].addedAt).isBetween(startDate, endDate));
  });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.