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;
CodePudding user response:
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)
CodePudding user response:
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)