好吧。。。我已经在这个问题上呆了一段时间
我试图获取每个领导者的(leaderDisplay(dateExit值的计数,该值等于=="并将其存储到阵列中
我试图得到的输出类似于[Albert Owens','Tina Snow','Rick Sanchez'][2,1,2]
这就是我迄今为止所取得的成就,我很确定我想得太多了…
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;
您可以使用reduce
来计算对象中每个leaderDisplay
的空dateExits
的数量。如果要生成两个数组(名称和值(,只需使用Object.keys
和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)
这类用例是Array.prototype.reduce非常方便的用例。它对数组中的所有元素进行迭代,并相应地修改累加器。由于数据中的每个元素只包含fieldData
,因此参数中使用了解构({ fieldData:elem }
(,在处理之前将fieldData
从每个元素中提取为elem
。
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)