Home > OS >  How do I get the total number of occurrence for each array in an object?
How do I get the total number of occurrence for each array in an object?

Time:09-23

i currently have this object in React and i can't seem to get the output i want, i am not sure if i have structured the object properly. The data are retrieved using Firestore. I have stored the results in to this summary array and tried working on it

const summary = [
        {
          data: [
            {
              date: "Sep 21 2021",
              prodID: "005",
            },
          ],
        },
        {
          data: [
            {
              date: "Sep 21 2021",
              prodID: "002",
            },
            {
              date: "Sep 21 2021",
              prodID: "002",
            },
          ],
        },
        {
          data: [
            {
              date: "Sep 21 2021",
              prodID: "004",
            },
            {
              date: "Sep 21 2021",
              prodID: "004",
            },
            {
              date: "Sep 21 2021",
              prodID: "004",
            },
            {
              date: "Sep 19 2021",
              prodID: "004",
            },
            {
              date: "Sep 19 2021",
              prodID: "004",
            },
          ],
        },   ];

Is it even possible to get this output.. I have been trying for countless of hours now.. how do i code it such that i will get a result like this?

[
    {
        "count": 1,
        "date": "Sep 21 2021",
        "prodID": "005"
    },
    {
        "count": 2,
        "date": "Sep 21 2021",
        "prodID": "002"
    },
    {
        "count": 3,
        "date": "Sep 21 2021",
        "prodID": "004"
    },
    {
        "count": 2,
        "date": "Sep 19 2019",
        "prodID": "004"
    }
]

This is the latest logic that i've tried

firebase
  .firestore()
  .collection("batch")
  .doc(ctx.currentUser.companyName)
  .collection("products")
  .get()
  .then((snapshot) => {
    let arr = [];
    snapshot.forEach((doc) => {
      const dt = doc.data().prodID;
      if (!arr[dt]) {
        arr[dt] = 1;
      } else {
        arr[dt]  = 1;
      }
    });

    for (const t in arr) {
      firebase
        .firestore()
        .collection("batch")
        .doc(ctx.currentUser.companyName)
        .collection("products")
        .where("prodID", "==", t)
        .orderBy("dateAdded", "desc")
        .get()
        .then((snapshot) => {
          let arr2 = [];
          let c = 0;
          snapshot.forEach((doc) => {
            const dt = getDate(doc.data().dateAdded["seconds"]);
            if (!arr2[c]) {
              arr2[c] = { count: 1, date: dt, prodID: t };
            } else {
              console.log("in");
              if (arr2.prodID === t) {
                for (const d in arr2) {
                  if (arr2[d].count) {
                    arr2[d].count  ;
                  }
                }
              }
            }
            c  ;
          });
          let v = {};
          v["data"] = arr2;
          addSummary(v);
        });
    }
  });

This is the result i get after executing

for (let j = 0; j < summary.length; j  ) {
      for (let k = 0; k < summary[j].data.length; k  ) {
        console.log(summary[j].data[k]);
      }
    }

console result

CodePudding user response:

Iterate over data and for each unique date (new Set(d.data.map(prod => prod.date))) add to result the count (d.data.filter(prod => prod.date === date).length)

const summary = [{
    data: [{
      date: "Sep 21 2021",
      prodID: "005",
    }, ],
  },
  {
    data: [{
        date: "Sep 21 2021",
        prodID: "002",
      },
      {
        date: "Sep 21 2021",
        prodID: "002",
      },
    ],
  },
  {
    data: [{
        date: "Sep 21 2021",
        prodID: "004",
      },
      {
        date: "Sep 21 2021",
        prodID: "004",
      },
      {
        date: "Sep 21 2021",
        prodID: "004",
      },
      {
        date: "Sep 19 2021",
        prodID: "004",
      },
      {
        date: "Sep 19 2021",
        prodID: "004",
      },
    ],
  },
];
const result = []
summary.forEach(d => {
    let dates = new Set(d.data.map(prod => prod.date))
    dates.forEach(date => {
      result.push({
        date: date,
        prodId: d.data[0].prodID,
        count: d.data.filter(prod => prod.date === date).length
      })
    })
})
console.log(result)
Assuming summary is equal to snapshot your code should/could look like:

firebase
  .firestore()
  .collection("batch")
  .doc(ctx.currentUser.companyName)
  .collection("products")
  .get()
  .then((snapshot) => {
    const result = []
    snapshot.forEach(doc => {
        let dates = new Set(doc.data.map(prod => prod.date))
        dates.forEach(date => {
          result.push({
            date: date,
            prodId: doc.data[0].prodID,
            count: doc.data.filter(prod => prod.date === date).length
          })
        })
    })
    console.log(result)
  })
  • Related