使用Node.js在DynamoDB中过滤嵌套对象数组查询全部记录
筛选包含Intern晋升记录的HR人员
首先修正原HR数据中的语法错误(部分对象属性间缺少逗号),修正后的数据如下:
const HR = { id: "11", name: "Ahmad", designation: "CEO", promotion_history: [ { "title": "CFO", "comment": "based on better performance", "id": 356 }, { "title": "Specialist", "comment": "Promoted with panel approval", "id": 244 }, { "title": "Officer", "comment": "Completed internship successfully", "id": 234 }, { "title": "Intern", "comment": "Selected based on extra-ordinary academic background", "id": 112 } ] }
筛选逻辑实现
要筛选出promotion_history中包含title为"Intern"的人员,可以使用数组的some()方法快速判断:
// 判断单条HR记录是否符合条件 const hasInternHistory = HR.promotion_history.some(item => item.title === "Intern"); // 如果是多条HR记录的数组,筛选出所有符合条件的人员 const hrList = [HR]; // 示例HR人员数组 const filteredHR = hrList.filter(person => person.promotion_history.some(item => item.title === "Intern") );
代码说明
some()方法会遍历promotion_history数组,只要找到任意一项的title等于"Intern",就返回true,表示该人员符合筛选条件。- 若处理多条人员记录,直接用
filter()结合some()即可得到所有符合要求的人员列表。
内容的提问来源于stack exchange,提问作者H. Pardess
相关产品推荐
相关产品推荐

