Javascript 通过在JSON数据中查找值来创建新的对象数组
我不确定我能做到这一点,但我正在尝试创建一个新数组,通过搜索一些JSON数据来找到一个键,如果找到了键,则将整个对象放入新数组。下面是我正在使用的数据示例Javascript 通过在JSON数据中查找值来创建新的对象数组,javascript,arrays,json,object,Javascript,Arrays,Json,Object,我不确定我能做到这一点,但我正在尝试创建一个新数组,通过搜索一些JSON数据来找到一个键,如果找到了键,则将整个对象放入新数组。下面是我正在使用的数据示例 { "students": { "AB10001": { "campus": "cda", "subjects": ["history", "english"], } "AB10002": { "campus": "asd", "subjects": ["maths"
{
"students": {
"AB10001": {
"campus": "cda",
"subjects": ["history", "english"],
}
"AB10002": {
"campus": "asd",
"subjects": ["maths"],
}
"AB10003": {
"campus": "asd",
"subjects": ["english"],
}
"AB10004": {
"campus": "asd",
"subjects": ["history"],
}
"AB10005": {
"campus": "cda",
"subjects": ["maths", "science"],
}
"AB10006": {
"campus": "asd",
"subjects": ["science"],
}
"AB10007": {
"campus": "cda",
"subjects": ["science"],
}
"AB10008": {
"campus": "asd",
"subjects": ["science", "history"],
}
"AB10009": {
"campus": "cda",
"subjects": ["history"],
}
"AB10010": {
"campus": "cda",
"subjects": ["history", "maths"],
}
}
}
所以我想做的是通过主题的students键搜索所有做历史的学生,然后从中创建另一个数组,取整个学生,同时保持原始对象不变
所以我想以这样的方式结束:
{
"historyStudents": {
"AB10001": {
"campus": "cda",
"subjects": ["history", "english"],
}
"AB10004": {
"campus": "asd",
"subjects": ["history"],
}
"AB10008": {
"campus": "asd",
"subjects": ["science", "history"],
}
"AB10009": {
"campus": "cda",
"subjects": ["history"],
}
"AB10010": {
"campus": "cda",
"subjects": ["history", "maths"],
}
}
}
任何见解或帮助都会非常有用。提前谢谢 您可以使用Object.keys()
和reduce()
创建新的对象,只让学生具有历史
科目
var数据={“学生”:{“AB10001”:{“校园”:“cda”,“科目”:[“历史”,“英语”]},“AB10002”:{“校园”:“asd”,“科目”:[“数学”]},“AB10003”:{“校园”:“asd”,“科目”:[“英语”]},“AB10004”:{“校园”:“asd”,“科目”:[“历史”},“AB10005”:{“校园”:“cda”,“科目”:[“数学”,“科学”},“AB10006”:{“校园”:“asd”,“科目”[“科学”},“AB10007”:{“校园”:“cda”,“学科”:[“科学”]},“AB10008”:{“校园”:“asd”,“学科”:[“科学”,“历史”]},“AB10009”:{“校园”:“cda”,“学科”:[“历史”]},“AB10010”:{“校园”:“cda”,“学科”:[“历史”,“数学”}
var结果={
historyStudents:Object.key(data.students)
.减少(功能(r,e){
if(data.students[e].subjects.includes('history'))r[e]=data.students[e]
返回r;
}, {})
}
console.log(result)
您可以获取键,对其进行筛选,然后根据键和值构建一个新对象。然后使用创建单个对象
函数getStudents(对象、主题){
返回Object.assign({},…)对象
.keys(对象)
.filter(k=>object[k]。subject.includes(subject))
.map(k=>({[k]:对象[k]}))
);
}
var object={学生:{AB10001:{校园:“cda”,科目:[“历史”,“英语”]},AB10002:{校园:“asd”,科目:[“数学”]},AB10003:{校园:“asd”,科目:[“英语”]},AB10004:{校园:“asd”,科目:[“历史”]},AB10005:{校园:“cda”,科目:[“数学”,“科学”]},AB10006:{校园:“asd”,科目:[“科学”}AB10007:{校园:“cda”,科目:[“科学”]},AB10008:{校园:“asd”,科目:[“科学”,“历史”]},AB10009:{校园:“cda”,科目:[“历史”]},AB10010:{校园:“cda”,科目:[“历史”,“数学”},
结果={historyStudents:getStudents(object.students,'history');
console.log(结果);
。作为控制台包装{max height:100%!important;top:0;}
除了上述答案之外,您还可以es6解构
以获得所需的结果
const data = {"students":{"AB10001":{"campus":"cda","subjects":["history","english"]},"AB10002":{"campus":"asd","subjects":["maths"]},"AB10003":{"campus":"asd","subjects":["english"]},"AB10004":{"campus":"asd","subjects":["history"]},"AB10005":{"campus":"cda","subjects":["maths","science"]},"AB10006":{"campus":"asd","subjects":["science"]},"AB10007":{"campus":"cda","subjects":["science"]},"AB10008":{"campus":"asd","subjects":["science","history"]},"AB10009":{"campus":"cda","subjects":["history"]},"AB10010":{"campus":"cda","subjects":["history","maths"]}}}
const historyStudents = {};
for(const stdId in data.students) {
// es6 destructuring
const { subjects } = data.students[stdId];
if(subjects.includes('history')){
historyStudents[stdId] = data.students[stdId];
}
}
// object property shorthand
console.log({ historyStudents });
您只需使用Object.keys()
循环遍历数据.students
键,并为每个键检查相对对象值在其subjectcs
数组中是否具有历史记录
:
var result = {
"historyStudents": {}
};
Object.keys(data.students).forEach(function(k) {
if (data.students[k].subjects.indexOf("history") > -1) {
result.historyStudents[k] = data.students[k];
}
});
演示:
var数据={
“学生”:{
“AB10001”:{
“校园”:“cda”,
“科目”:[“历史”、“英语”],
},
“AB10002”:{
“校园”:“asd”,
“科目”:[“数学”],
},
“AB10003”:{
“校园”:“asd”,
“主题”:[“英语”],
},
“AB10004”:{
“校园”:“asd”,
“主题”:[“历史”],
},
“AB10005”:{
“校园”:“cda”,
“科目”:[“数学”、“科学”],
},
“AB10006”:{
“校园”:“asd”,
“学科”:[“科学”],
},
“AB10007”:{
“校园”:“cda”,
“学科”:[“科学”],
},
“AB10008”:{
“校园”:“asd”,
“学科”:[“科学”、“历史”],
},
“AB10009”:{
“校园”:“cda”,
“主题”:[“历史”],
},
“AB10010”:{
“校园”:“cda”,
“科目”:[“历史”、“数学”],
}
}
};
var结果={
“历史学生”:{}
};
Object.key(data.students).forEach(函数(k){
if(data.students[k].subjects.indexOf(“history”)>-1){
result.historyStudents[k]=data.students[k];
}
});
console.log(结果)
Hi,我会帮你。你能给我一些更多的上下文吗?我看到你的关键词包括javascript。这是你选择的语言来使用json并搜索它并生成新的JSONTANKS Nenad,这与我尝试的路线类似,尽管这种方法生成的结果与我得到的结果相同。结果的问题是学生ID没有被发送到新对象中。如果您将得到的结果与我想要的结果进行比较,结果类似,但不相同。@NenadVracar Yes现在它返回准确的结果,正确的做法是在键上循环。