1

Alright... I've been stuck on this for a while

I'm trying to grab the count of the each leader's(leaderDisplay) dateExit value that equals === "" and store it into an array

The output im trying to get is something like ['Albert Owens','Tina Snow','Rick Sanchez'] [2,1,2]

This is where I've gotten so far and I'm pretty sure i'm over thinking it...


const data = [
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Tina Snow",
    },
  },
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Rick Sanchez",
    },
  },
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Albert Owens",
    },
  },
  {
    fieldData: {
      dateExit: "07/14/2006",
      dateHire: "06/14/2004",
      leaderDisplay: "Tina Snow",
    },
  },
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Albert Owens",
    },
  },
  {
    fieldData: {
      dateExit: "07/14/2006",
      dateHire: "06/14/2004",
      leaderDisplay: "Rick Sanchez",
    },
  },
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Rick Sanchez",
    },
  },
];

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}

const allExit = data.map(function (e) {
  return e.fieldData.dateExit;
});

const dateExits = allExit.filter(onlyUnique);

const allSupervisors = data.map(function (e) {
  return e.fieldData.leaderDisplay;
});
const supervisors = allSupervisors.filter(onlyUnique);

let array = [];
let countArr = [];
let textColors = [];

supervisors.forEach(function (f, i) {
  let count = 0;
  if (f != "") {
    let stat = f;

    if (stat == "") {
      countArr.push(count);
    }
    let obj = {
      name: stat,
    };
    let keysArr = [];
    let statArr = [];
    supervisors.forEach(function (e, i) {
 
      let sup = e;
      let list = data
        .filter(function (e) {
          return (
            e.fieldData.leaderDisplay === sup &&
            e.fieldData.dateExit === ""
          );
        })
        .map(function (e) {
          return e.fieldData.ID;
        });

      statArr.push(list.length);
      keysArr.push(list);
    });
  
    if (Array.isArray(statArr) && statArr.length > 0) {
      obj.data = statArr;
      obj.keys = keysArr;
    }

    obj.keys;
    array.push(obj);

    return array;
  }

  return array;
});

const categories = supervisors;

const series = array;

2 Answers 2

2

You can use reduce to count the number of empty dateExits for each leaderDisplay into an object. If you want to then produce two arrays (of names, and values), just use Object.keys and Object.values:

const data = [
  {
    fieldData: {
      dateExit: "", dateHire: "06/14/2004", leaderDisplay: "Tina Snow",
    },
  },
  {
    fieldData: {
      dateExit: "", dateHire: "06/14/2004", leaderDisplay: "Rick Sanchez",
    },
  },
  {
    fieldData: {
      dateExit: "", dateHire: "06/14/2004", leaderDisplay: "Albert Owens",
    },
  },
  {
    fieldData: {
      dateExit: "07/14/2006", dateHire: "06/14/2004", leaderDisplay: "Tina Snow",
    },
  },
  {
    fieldData: {
      dateExit: "", dateHire: "06/14/2004", leaderDisplay: "Albert Owens",
    },
  },
  {
    fieldData: {
      dateExit: "07/14/2006", dateHire: "06/14/2004", leaderDisplay: "Rick Sanchez",
    },
  },
  {
    fieldData: {
      dateExit: "", dateHire: "06/14/2004", leaderDisplay: "Rick Sanchez",
    },
  },
];

const result = data.reduce((acc, obj) => {
  const { leaderDisplay, dateExit } = obj.fieldData;
  acc[leaderDisplay] = (acc[leaderDisplay] || 0) + (dateExit === "")
  return acc
}, {})

console.log(result)

const names = Object.keys(result)
const values = Object.values(result)
console.log(names)
console.log(values)

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

Comments

1

This sort of use case is one where Array.prototype.reduce comes in very handy. It iterates over all elements in an Array and modifies the accumulator accordingly. Since each elemement in your data only contains fieldData, deconstruction ({ fieldData:elem }) has been used in the parameters to pull fieldData out of each element as elem before processing.

data = [
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Tina Snow",
    },
  },
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Rick Sanchez",
    },
  },
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Albert Owens",
    },
  },
  {
    fieldData: {
      dateExit: "07/14/2006",
      dateHire: "06/14/2004",
      leaderDisplay: "Tina Snow",
    },
  },
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Albert Owens",
    },
  },
  {
    fieldData: {
      dateExit: "07/14/2006",
      dateHire: "06/14/2004",
      leaderDisplay: "Rick Sanchez",
    },
  },
  {
    fieldData: {
      dateExit: "",
      dateHire: "06/14/2004",
      leaderDisplay: "Rick Sanchez",
    },
  },
]

let accumulator = {}

results = data.reduce( ( accum, { fieldData:elem } ) => {
  accum[elem.leaderDisplay] = (accum[elem.leaderDisplay] || 0) + ((elem.dateExit === "") ?  1 : 0);
  return accum
}, accumulator);

console.log(results)

1 Comment

Right? Reduce takes some getting used to, but is powerful

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.