JavaScript判断索引是否在目标数组并提取字段存入新数组问题
问题场景
现有JSON结构包含fields字段元数据和records学校数据数组,需要提取每条学校记录中索引在target_indices数组内的对应字段,整理后存入schools变量,原代码在判断索引是否属于目标列表的逻辑处运行不符合预期。
原始JSON片段
{ "fields": [{"type":"int","id":"_id"},{"type":"text","id":"Centre Code"},{"type":"text","id":"Centre Name"},{"info":{"notes":"","type_override":"","label":""},"type":"text","id":"Centre Type"},{"type":"text","id":"Centre Status"},{"type":"text","id":"Host Centre Code"},{"type":"text","id":"Host Centre Name"},{"type":"text","id":"Official Low Year Level"},{"type":"text","id":"Official High Year Level"},{"type":"text","id":"Officer In Charge Title"},{"type":"numeric","id":"School Band"},{"type":"timestamp","id":"Show Holiday Date"},{"type":"text","id":"Internet Site"},{"type":"text","id":"Phone Number"},{"type":"text","id":"Restrict Contact Outside Teaching Hours"},{"type":"text","id":"Fax Number"},{"type":"text","id":"Actual Address Line 1"},{"type":"text","id":"Actual Address Line 2"},{"type":"text","id":"Actual Address Line 3"},{"type":"numeric","id":"Actual Address Post Code"},{"type":"text","id":"Postal Address Line 1"},{"type":"text","id":"Postal Address Line 2"},{"type":"text","id":"Postal Address Line 3"},{"type":"numeric","id":"Postal Address Post Code"},{"type":"text","id":"Education Geographic Region"},{"type":"text","id":"Federal Electorate"},{"type":"text","id":"State Electorate"},{"type":"text","id":"Local Government Area"},{"type":"text","id":"Statistical Area Level2"},{"type":"numeric","id":"Statistical Area Level2 Code"},{"type":"text","id":"Remoteness Area"},{"type":"text","id":"Enrolment Effective Date"},{"type":"numeric","id":"All Student Count"},{"type":"text","id":"Campus All Student Count"},{"type":"numeric","id":"ABN"},{"info":{"notes":"","type_override":"","label":""},"type":"text","id":"Sector"},{"type":"text","id":"Non-State Sector"},{"type":"numeric","id":"Longitude"},{"type":"numeric","id":"Latitude"}], "records": [ [2,"0591","Abercorn State School","State School","Open","","","Prep Year","Year 6","Principal",5,"2020-08-10T00:00:00","www.abercornss.eq.edu.au","(07) 4167 5190","Y","(07) 4167 5135","","957 Wuruma Dam Road","Abercorn",4627,"957 Wuruma Dam Road","Abercorn","",4627,"Central Queensland","Flynn","Callide","North Burnett (R)","Monto - Eidsvold",319021508,"Outer Regional Australia","2019 July",18,"",22101246877,"State","",151.127031,-25.135955], [3,"1275","Abergowrie State School","State School","Open","","","Prep Year","Year 6","Principal",5,"2020-07-03T00:00:00","www.abergowriess.eq.edu.au","(07) 4777 4672","N","(07) 4777 4686","","5 Venables Road","Abergowrie",4850,"5 Venables Road","Abergowrie","",4850,"North Queensland","Kennedy","Hinchinbrook","Hinchinbrook (S)","Ingham Region",318011465,"Remote Australia","2019 July",4,"",87244066343,"State","",145.88351,-18.474697] ] }
原有错误代码
fetch('./qldopendata-json/school_locations.json') .then(response => { return response.json(); }) .then(schoolData => { const schools = []; const target_indices = [2,7,8,12,13,17,18,19,37,38]; schoolData.records.forEach((school_value) => { const tidied_school = {}; schoolData.fields.forEach((field_name, field_index) => { tidied_school[field_index] = school_value[field_index] if (field_index in target_indices){ //this is where i need help schools.push(tidied_school); } }) console.log(schools); }) })
修正方案
问题说明
原代码存在两处核心错误:
in操作符用于判断属性是否存在于对象中,不能用来判断值是否存在于数组内,判断数组包含指定值应该用Array.includes()方法schools.push()位置错误,每匹配到一个字段就推入一次数组会导致单条学校记录被重复添加多次,应该整理完整条学校的目标字段后再推入
可运行修正代码
fetch('./qldopendata-json/school_locations.json') .then(response => response.json()) .then(schoolData => { const schools = []; const target_indices = [2,7,8,12,13,17,18,19,37,38]; schoolData.records.forEach(school_value => { const tidied_school = {}; schoolData.fields.forEach((field_meta, field_index) => { // 仅当索引在目标列表中时才存入字段,默认用字段名作为key更易读 if(target_indices.includes(field_index)){ tidied_school[field_meta.id] = school_value[field_index]; // 如果需要保留索引作为key,替换成下面这行即可 // tidied_school[field_index] = school_value[field_index]; } }) // 整理完单条学校所有字段后再推入结果数组 schools.push(tidied_school); }) console.log(schools); })
内容的提问来源于stack exchange,提问作者scrape_noob
相关产品推荐
相关产品推荐

