在数组中查找元素并将其添加到另一个javascript中



下面是我正在处理的JSON文件的一个片段,位于数组中"target_indices"你会看到我想要提取的数据的索引。如果field_index中有我的目标索引,我似乎很难匹配,我想把它添加到const学校。如有任何帮助,我们将不胜感激!

"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);
})

})  

您可以使用reduce((。

以下示例:

const schoolData = { 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, ], ], };
const targetIndices = [2, 7, 8, 12, 13, 17, 18, 19, 37, 38];
const schools = schoolData.records.reduce((a, b) => {
const requiredObject = targetIndices.reduce((acc, t) => {
acc.push(b[t]);
return acc;
}, []);
a.push(requiredObject);
return a;
}, []);
console.log(schools);

希望这能帮到你。您需要手动创建target_indices_names,可能还有另一种动态方法,但由于它只有10个属性。

var schoolData = '[[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]]'
var parsedSchoolData = JSON.parse(schoolData);
const schools = [];
const target_indices_names = ['one','two','three','four','five','six','seven','eight', 'nine', 'ten'];
const target_indices = [2,7,8,12,13,17,18,19,37,38];
parsedSchoolData.forEach(element => {
// New Object
var obj = new Object();
// Pass through all Target Indices
target_indices.forEach(neededIndex => {
console.log(neededIndex);
var propertyName = target_indices_names[target_indices.indexOf(neededIndex)];
// Set Property to Value
obj[propertyName] = element[neededIndex];
});
// Push/Add the created Object to the list
schools.push(obj);
});
console.log(schools[0]);

最新更新