我有一个包含对象的数组,我正在试图弄清楚如何过滤掉一些我不需要它们的对象。我试图在字段code
的基础上过滤掉它。下面是我到目前为止尝试过的代码,它会抛出错误a.filter is not a function
。在这种情况下我能做些什么?过滤器不是正确的方法。谢谢。
var chartsArray = [
{
"name": "Total Educated",
"code": "Q035001",
"parent": "EDU_ATTAINMENT",
"value": "9900",
"label": "Total Educated",
"children": []
},
{
"name": "Grade Less than 9",
"code": "Q035003",
"parent": "EDU_ATTAINMENT",
"value": "369",
"label": "Grade 9",
"children": []
},
{
"name": "Grade 9 to 12",
"code": "Q035007",
"parent": "EDU_ATTAINMENT",
"value": "595",
"label": "Grade 9 - 12",
"children": []
},
{
"name": "High School",
"code": "Q035011",
"parent": "EDU_ATTAINMENT",
"value": "1174",
"label": "High School",
"children": []
},
{
"name": "Some College",
"code": "Q035012",
"parent": "EDU_ATTAINMENT",
"value": "1904",
"label": "Some College",
"children": []
},
{
"name": "College Degree - Associate's",
"code": "Q035014",
"parent": "EDU_ATTAINMENT",
"value": "436",
"label": "Associate's",
"children": []
},
{
"name": "College Degree - Bachelor's",
"code": "Q035015",
"parent": "EDU_ATTAINMENT",
"value": "2999",
"label": "Bachelor's",
"children": []
},
{
"name": "College Degree - Master's",
"code": "Q035016",
"parent": "EDU_ATTAINMENT",
"value": "1763",
"label": "Master's",
"children": []
},
{
"name": "College - Professional",
"code": "Q035017",
"parent": "EDU_ATTAINMENT",
"value": "413",
"label": "Professional",
"children": []
},
{
"name": "College Degree - Doctorate",
"code": "Q035018",
"parent": "EDU_ATTAINMENT",
"value": "246",
"label": "Doctorate",
"children": []
},
{
"name": "Enrollments (Total Population)",
"code": "EDU_Enrollments",
"parent": "EDU_ATTAINMENT",
"label": "Enrollments (Total Population)",
"children": [
{
"name": "Nursery school/Preschool",
"code": "Q036003",
"parent": "EDU_Enrollments",
"value": "269",
"label": "Nursery school/Preschool",
"children": []
},
{
"name": "Kindergarten/Elementary school",
"code": "Q036006",
"parent": "EDU_Enrollments",
"value": "1156",
"label": "Kindergarten/Elementary school",
"children": []
},
{
"name": "High School",
"code": "Q036015",
"parent": "EDU_Enrollments",
"value": "539",
"label": "High School",
"children": []
},
{
"name": "College/Graduate /Professional school",
"code": "Q036018",
"parent": "EDU_Enrollments",
"value": "1869",
"label": "College/Graduate /Professional school",
"children": []
},
{
"name": "Not Enrolled",
"code": "Q036024",
"parent": "EDU_Enrollments",
"value": "10380",
"label": "Not Enrolled",
"children": []
}
]
},
{
"name": "Percents",
"code": "PCT_EDU_ATTAINMENT",
"parent": "EDU_ATTAINMENT",
"label": "Percents",
"children": [
{
"name": "% Grade Less than 9",
"code": "XQ035003",
"parent": "PCT_EDU_ATTAINMENT",
"value": "3.7231",
"label": "% Grade 9",
"children": []
},
{
"name": "% Grade 9 to 12",
"code": "XQ035007",
"parent": "PCT_EDU_ATTAINMENT",
"value": "6.0112",
"label": "% Grade 9 - 12",
"children": []
},
{
"name": "% High school",
"code": "XQ035011",
"parent": "PCT_EDU_ATTAINMENT",
"value": "11.8622",
"label": "% High school",
"children": []
},
{
"name": "% Some college",
"code": "XQ035012",
"parent": "PCT_EDU_ATTAINMENT",
"value": "19.2332",
"label": "% Some college",
"children": []
},
{
"name": "% College - Associate",
"code": "XQ035014",
"parent": "PCT_EDU_ATTAINMENT",
"value": "4.4073",
"label": "Associate",
"children": []
},
{
"name": "% College - Bachelors",
"code": "XQ035015",
"parent": "PCT_EDU_ATTAINMENT",
"value": "30.2966",
"label": "Bachelors",
"children": []
},
{
"name": "% College - Masters",
"code": "XQ035016",
"parent": "PCT_EDU_ATTAINMENT",
"value": "17.8096",
"label": "Masters",
"children": []
},
{
"name": "% College - Professional",
"code": "XQ035017",
"parent": "PCT_EDU_ATTAINMENT",
"value": "4.168",
"label": "Professional",
"children": []
},
{
"name": "% College - Doctorate",
"code": "XQ035018",
"parent": "PCT_EDU_ATTAINMENT",
"value": "2.4888",
"label": "Doctorate",
"children": []
}
]
}
];
var el=[
"Q035001",
"PCT_EDU_ATTAINMENT"
];
output = chartsArray = chartsArray.map(a => a.filter(function code(o) {
if (!el.includes(o.code)) {
if (o.children) {
o.children = o.children.filter(code);
}
return true;
}
}));
console.log(output)

答案 0 :(得分:2)
chartsArray
是一个对象数组。
执行chartsArray.map(a => a.filter(...))
时,
这意味着对于数组中的每个对象a
,请调用filter
。
这是不正确的,因为对象没有filter
方法。
您似乎打算在数组上使用filter
,而不是map
:
output = chartsArray = chartsArray.filter(function code(o) {
if (!el.includes(o.code)) {
if (o.children) {
o.children = o.children.filter(code);
}
return true;
}
});
答案 1 :(得分:0)
您可以使用递归递归过滤:
const filter = [
"Q035001",
"PCT_EDU_ATTAINMENT"
];
function filterBy(arr, filter){
return arr.filter( obj => {
obj.children = filterBy(obj.children, filter);
return !filter.includes(obj code);
});
}
const output = filterBy(chartsArray, filter);