gpt4 book ai didi

javascript - 如果每个项目都投资了数年和数月,如何实现按对象按值(value)搜索

转载 作者:行者123 更新时间:2023-11-28 14:18:14 25 4
gpt4 key购买 nike

我做了一个网站,但我无法通过多维数组/对象进行搜索(我不知道有什么区别)。

在添加年份和月份之前,我直接通过标识符 array[id][field] 访问它们

如何实现多维数组中嵌套结构的搜索

我尝试在界面中使用其他字段预先定义年份和月份。它看起来令人毛骨悚然且难以理解。

{
"2018": {
"Aug": {
"1": {
"id": 1,
"appeal_date": "2018-08-24",
"city_of_residence": "aaa",
"patient_name": "John",
"patient_age": 62,
"coordinator": "aaa",
"source": "aaa",
"birth_date": "1956-06-30",
"contact_person_name": "",
"contact_person_role": "",
"contact_person_phones": [
"",
""
],
"diagnosis": "aasasd",
"status": "9",
"departure_date": "2016-02-18",
"arrival_date": "2020-01-23",
"patient_phones": [
"",
""
],
"editable": true
}
}
},
"2019": {
"Oct": {
"65": {
"id": 65,
"appeal_date": "2019-10-18",
"city_of_residence": "asfsac",
"patient_name": "asvsas",
"patient_age": 62,
"coordinator": "",
"source": "asfasfa",
"birth_date": "1956-06-30",
"contact_person_name": "",
"contact_person_role": "",
"contact_person_phones": "",
"diagnosis": "assdbcx",
"status": "1",
"departure_date": "",
"arrival_date": "",
"patient_phones": "",
"editable": true
}
},
"Jun": {
"64": {
"id": 64,
"appeal_date": "2019-06-04",
"city_of_residence": "afsfsa",
"patient_name": "asvac",
"patient_age": 62,
"coordinator": "",
"source": "agwdawdawd",
"birth_date": "1956-06-30",
"contact_person_name": "",
"contact_person_role": "",
"contact_person_phones": "",
"diagnosis": "agsags",
"status": "1",
"departure_date": "",
"arrival_date": "",
"patient_phones": "",
"editable": true
}
}
}
}

我需要的只是更新正确对象中的一些字段

patients [2019] ['Oct'] [65] ['diagnosis'] = 'aaaaa'

类似这样的事情,但在某个月份患者可能不正确

我想要这样的东西:

patients.nestingSearchByKey (65) ['diagnosis'] = 'aaaaa'

它们的标识符是唯一的且不重复

我为我的英语道歉

请提出任何想法

最佳答案

您可以使用Object.keys来获取对象中的键数组。

var pts = {
"2018": {
"Aug": {
"1": {
"id": 1,
"appeal_date": "2018-08-24",
"city_of_residence": "aaa",
"patient_name": "John",
"patient_age": 62,
"coordinator": "aaa",
"source": "aaa",
"birth_date": "1956-06-30",
"contact_person_name": "",
"contact_person_role": "",
"contact_person_phones": [
"",
""
],
"diagnosis": "aasasd",
"status": "9",
"departure_date": "2016-02-18",
"arrival_date": "2020-01-23",
"patient_phones": [
"",
""
],
"editable": true
}
}
},
"2019": {
"Oct": {
"65": {
"id": 65,
"appeal_date": "2019-10-18",
"city_of_residence": "asfsac",
"patient_name": "asvsas",
"patient_age": 62,
"coordinator": "",
"source": "asfasfa",
"birth_date": "1956-06-30",
"contact_person_name": "",
"contact_person_role": "",
"contact_person_phones": "",
"diagnosis": "assdbcx",
"status": "1",
"departure_date": "",
"arrival_date": "",
"patient_phones": "",
"editable": true
}
},
"Jun": {
"64": {
"id": 64,
"appeal_date": "2019-06-04",
"city_of_residence": "afsfsa",
"patient_name": "asvac",
"patient_age": 62,
"coordinator": "",
"source": "agwdawdawd",
"birth_date": "1956-06-30",
"contact_person_name": "",
"contact_person_role": "",
"contact_person_phones": "",
"diagnosis": "agsags",
"status": "1",
"departure_date": "",
"arrival_date": "",
"patient_phones": "",
"editable": true
}
}
}
}

function search(patients, id) {
var patient;
const years = Object.keys(patients);
years.forEach(year => {
const months = Object.keys(patients[year]);
months.forEach(month => {
if (patients[year][month][id]) {
patient = patients[year][month][id];
}
});
});

return patient;
}

const p = search(pts, 64);
if (p) {
p['status'] = 0;
console.log(pts);
} else {
console.log('Not find patient with id 64');
}

关于javascript - 如果每个项目都投资了数年和数月,如何实现按对象按值(value)搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56457567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com