2017-11-09 13 views
1

私はオブジェクトを持つ配列を持っていますが、私はそれらを必要としないいくつかのオブジェクトをフィルタリングする方法を理解しようとしています。私はフィールドcodeに基づいてそれをフィルタリングしようとしています。以下は私が今まで試したコードで、エラーa.filter is not a functionをスローします。この場合、私は何ができますか。フィルターはそれを行う正しい方法ではありません。ありがとう。JavaScriptの配列内のオブジェクトをフィルタリングする方法

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)

chartsArray
+0

@SamHartmanはい – sten

答えて

1

オブジェクトの配列です。 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; 
    } 
}); 
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); 
関連する問題