const data = [
{
groups: [
{
"raceEthnicity": {
"Black or African American": 45,
"American Indian or Alaska Native": 18,
"Asian - - East Asian": 27,
"White - Arabic/North African": 7,
"White - White/Caucasian/European": 142,
"Mixed/Other/Unspecified": 3
}
},
{
"raceEthnicity": {
"Black or African American": 48,
"American Indian or Alaska Native": 14,
"Asian - East Asian": 36,
"Asian - South East Asian": 3,
"White - Arabic/North African": 4,
"White - White/Caucasian/European": 140,
"Mixed/Other/Unspecified": 2
}
}
]
},
{
"groups": [
{
"raceEthnicity": {
"American Indian or Alaska Native": 11,
"Black or African American": 47,
"Asian - South East Asian": 0,
"White - Arabic/North African": 1,
"White - White/Caucasian/European": 9,
"Mixed/Other/Unspecified": 1
},
},
{
"raceEthnicity": {
"American Indian or Alaska Native": 4,
"Black or African American": 28,
"Asian - South East Asian": 2,
"White - Arabic/North African": 0,
"White - White/Caucasian/European": 9,
"Mixed/Other/Unspecified": 1
},
}
]
},
{
"groups": [
{
"raceEthnicity": {
"American Indian or Alaska Native": 11,
"Black or African American": 47,
"Asian - South East Asian": 0,
"White - Arabic/North African": 1,
"White - White/Caucasian/European": 9,
"Mixed/Other/Unspecified": 1
},
},
{
"raceEthnicity": {
"American Indian or Alaska Native": 4,
"Black or African American": 28,
"Asian - South East Asian": 2,
"White": 20,
"Mixed/Other/Unspecified": 1
},
}
]
}
]
大家好,我有一个对象,它看起来像这样,我正在训练编写一个过滤器,它接受像["Asian", "White"]这样的值数组,并返回具有["Asian", "White"] "0 . 所以如果对象种族有亚洲-东亚,应该选择,因为亚洲-东亚也包含亚洲人。
const data = [
{
groups: [
{
"raceEthnicity": {
"Black or African American": 45,
"American Indian or Alaska Native": 18,
"Asian - - East Asian": 0,
"White - Arabic/North African": 7,
"White - White/Caucasian/European": 142,
"Mixed/Other/Unspecified": 3
}
},
{
"raceEthnicity": {
"Black or African American": 45,
"American Indian or Alaska Native": 18,
"Asian - - East Asian": 27,
"White - Arabic/North African": 7,
"White - White/Caucasian/European": 142,
"Mixed/Other/Unspecified": 3
}
},
]
}
]
//Store the items whichs accomplish the conditions filterArrayValues
let valid = [];
//Always must be Array.
let filterArrayValues = ["Asian", "White"];
filterArrayValues.forEach((filter, index) => {
//Extract the groups of data
for (let { groups } of data) {
//Extract the raceEthnicity of groups
for (let { raceEthnicity } of groups) {
//Filter condition getting the key String
let filtered = Object.entries(raceEthnicity).filter(entry => {
/*
entry[0] is the string to check if includes "Asia" or whatever.
+entry[1] is the numeric which is evaluated with the condition > 0.
join is for get a string of filtered values.
*/
return entry[0].includes(filter) && +entry[1] > 0;
}).join();
// then got filtered data
if (filtered != "") {
//do the inverse to make up the object again
filtered.split(",").forEach((c, i, a) => {
if (i % 2 == 0) {
let object = {};
object[c] = +a[++i];
valid.push({
index,
filtered: object
})
}
});
}
}
}
});
console.dir(valid)
const data = [
{
groups: [
{
raceEthnicity: {
"Black or African American": 45,
"American Indian or Alaska Native": 18,
"Asian - - East Asian": 27,
"White - Arabic/North African": 7,
"White - White/Caucasian/European": 142,
"Mixed/Other/Unspecified": 3
}
},
{
raceEthnicity: {
"Black or African American": 48,
"American Indian or Alaska Native": 14,
"Asian - East Asian": 36,
"Asian - South East Asian": 3,
"White - Arabic/North African": 4,
"White - White/Caucasian/European": 140,
"Mixed/Other/Unspecified": 2
}
}
]
},
{
groups: [
{
raceEthnicity: {
"American Indian or Alaska Native": 11,
"Black or African American": 47,
"Asian - South East Asian": 0,
"White - Arabic/North African": 1,
"White - White/Caucasian/European": 9,
"Mixed/Other/Unspecified": 1
},
},
{
raceEthnicity: {
"American Indian or Alaska Native": 4,
"Black or African American": 28,
"Asian - South East Asian": 2,
"White - Arabic/North African": 0,
"White - White/Caucasian/European": 9,
"Mixed/Other/Unspecified": 1
},
}
]
},
{
groups: [
{
raceEthnicity: {
"American Indian or Alaska Native": 11,
"Black or African American": 47,
"Asian - South East Asian": 0,
"White - Arabic/North African": 1,
"White - White/Caucasian/European": 9,
"Mixed/Other/Unspecified": 1
},
},
{
raceEthnicity: {
"American Indian or Alaska Native": 4,
"Black or African American": 28,
"Asian - South East Asian": 2,
"White": 20,
"Mixed/Other/Unspecified": 1
},
}
]
}
]
const filterArr = keyWord => {
if (!keyWord) {
console.log("please enter a key word");
return;
}
return data.reduce((p, { groups }) => {
const tmpArr = groups.reduce((p, { raceEthnicity }) => {
const tmpObj = { raceEthnicity: {} };
for (const k in raceEthnicity) if (k.includes(keyWord) && raceEthnicity[k] > 0) {
tmpObj.raceEthnicity[k] = raceEthnicity[k];
}
return Object.keys(tmpObj.raceEthnicity).length > 0 ? p.concat(tmpObj) : p;
}, []);
return tmpArr.length > 0 ? p.concat({ groups: tmpArr }) : p;
}, [])
}
console.log(filterArr("Asian"));