js 遍历数组对象 根据属性值 生成新的数组对象

发布于 2022-09-13 00:48:28 字数 3514 浏览 20 评论 0

[
{
  "clockInState": 0,
  "sectionNum": "1",
  "courseBegin": "2019-02-09 00:00:00.0",
  "teacherName": "老师",
  "drillID": 0,
  "childList": [
    {
      "clockInState": 0,
      "sectionNum": "1",
      "courseBegin": "2019-02-09 00:00",
      "teacherName": "老师",
      "drillID": 0,
      "childList": [
        {
          "clockInState": 0,
          "sectionNum": "1",
          "courseBegin": "2019-02-09 00:00",
          "teacherName": "老师",
          "drillID": 0,
          "sectionId": 145,
          "sectionLevel": "3",
          "parentId": 131,
          "content": "章节看到了吗",
          "sectionName": "疾病类型",
          "courseDrillId": 10,
          "userSectionStats": 0,
          "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
        }
      ],
      "sectionId": 131,
      "sectionLevel": "2",
      "parentId": 55,
      "content": "day1内容",
      "sectionName": "day1",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
    }
  ],
  "sectionId": 55,
  "sectionLevel": "1",
  "parentId": 0,
  "content": "结合流行病学史、临床表现",
  "sectionName": "诊断",
  "courseDrillId": 10,
  "userSectionStats": 1,
  "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
},
{
  "clockInState": 0,
  "sectionNum": "2",
  "courseBegin": "2019-02-09 00:00:00.0",
  "teacherName": "老师",
  "drillID": 0,
  "childList": [
    {
      "clockInState": 0,
      "sectionNum": "14",
      "courseBegin": "2019-02-22 00:00",
      "teacherName": "老师",
      "drillID": 0,
      "childList": [
        {
          "sectionName": "打吊针",
          "clockInState": 0,
          "sectionNum": "1",
          "courseDrillId": 10,
          "courseBegin": "2019-02-22 00:00",
          "teacherName": "老师",
          "drillID": 0,
          "userSectionStats": 0,
          "sectionId": 210,
          "sectionLevel": "3",
          "courseCover": "courseCover/courseCover预防感冒1547003167675.png",
          "parentId": 209
        }
      ],
      "sectionId": 209,
      "sectionLevel": "2",
      "parentId": 207,
      "sectionName": "day1",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
    },
    {
      "clockInState": 0,
      "sectionNum": "15",
      "courseBegin": "2019-02-23 00:00",
      "teacherName": "老师",
      "drillID": 0,
      "childList": [
        {
          "sectionName": "吃西药",
          "clockInState": 0,
          "sectionNum": "1",
          "courseDrillId": 10,
          "courseBegin": "2019-02-23 00:00",
          "teacherName": "老师",
          "drillID": 0,
          "userSectionStats": 0,
          "sectionId": 402,
          "sectionLevel": "3",
          "courseCover": "courseCover/courseCover预防感冒1547003167675.png",
          "parentId": 236
        }
      ],
      "sectionId": 236,
      "sectionLevel": "2",
      "parentId": 207,
      "sectionName": "day2",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
    }
  ],
  "sectionId": 207,
  "sectionLevel": "1",
  "parentId": 0,
  "content": "undefined",
  "sectionName": "治疗",
  "courseDrillId": 10,
  "userSectionStats": 1,
  "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
}
]    

有这样一个结构的数组对象 和 如下结构的一维数组

const teachers = ['张三', '李四' …]

现在希望遍历数组对象,通过比对 teacherName 属性是否在 teachers 中存在,得到一个新的数组对象

遍历方法怎么写 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙气飘飘 2022-09-20 00:48:28

image.png

// 用js的filter方法,然后返回精准的数据
list.filter((item)=>{return item.teacherName === teaName})

image.png

// teaName 是用来做数据过滤的。
// 精准匹配:只有名字强等才会返回对应的数据信息
list.filter((item)=>{return item.teacherName === teaName})
// 模糊匹配:只要 teacherName包含对应的字符就会返回数据
list.filter((item)=>{return item.teacherName.indexOf(teaName) !== -1})
小伙你站住 2022-09-20 00:48:28
var list = [
{
  "clockInState": 0,
  "sectionNum": "1",
  "courseBegin": "2019-02-09 00:00:00.0",
  "teacherName": "老师",
  "drillID": 0,
  "childList": [
    {
      "clockInState": 0,
      "sectionNum": "1",
      "courseBegin": "2019-02-09 00:00",
      "teacherName": "老师",
      "drillID": 0,
      "childList": [
        {
          "clockInState": 0,
          "sectionNum": "1",
          "courseBegin": "2019-02-09 00:00",
          "teacherName": "张三",
          "drillID": 0,
          "sectionId": 145,
          "sectionLevel": "3",
          "parentId": 131,
          "content": "章节看到了吗",
          "sectionName": "疾病类型",
          "courseDrillId": 10,
          "userSectionStats": 0,
          "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
        }
      ],
      "sectionId": 131,
      "sectionLevel": "2",
      "parentId": 55,
      "content": "day1内容",
      "sectionName": "day1",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
    }
  ],
  "sectionId": 55,
  "sectionLevel": "1",
  "parentId": 0,
  "content": "结合流行病学史、临床表现",
  "sectionName": "诊断",
  "courseDrillId": 10,
  "userSectionStats": 1,
  "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
},
{
  "clockInState": 0,
  "sectionNum": "2",
  "courseBegin": "2019-02-09 00:00:00.0",
  "teacherName": "老师",
  "drillID": 0,
  "childList": [
    {
      "clockInState": 0,
      "sectionNum": "14",
      "courseBegin": "2019-02-22 00:00",
      "teacherName": "老师",
      "drillID": 0,
      "childList": [
        {
          "sectionName": "打吊针",
          "clockInState": 0,
          "sectionNum": "1",
          "courseDrillId": 10,
          "courseBegin": "2019-02-22 00:00",
          "teacherName": "老师",
          "drillID": 0,
          "userSectionStats": 0,
          "sectionId": 210,
          "sectionLevel": "3",
          "courseCover": "courseCover/courseCover预防感冒1547003167675.png",
          "parentId": 209
        }
      ],
      "sectionId": 209,
      "sectionLevel": "2",
      "parentId": 207,
      "sectionName": "day1",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
    },
    {
      "clockInState": 0,
      "sectionNum": "15",
      "courseBegin": "2019-02-23 00:00",
      "teacherName": "老师",
      "drillID": 0,
      "childList": [
        {
          "sectionName": "吃西药",
          "clockInState": 0,
          "sectionNum": "1",
          "courseDrillId": 10,
          "courseBegin": "2019-02-23 00:00",
          "teacherName": "李四",
          "drillID": 0,
          "userSectionStats": 0,
          "sectionId": 402,
          "sectionLevel": "3",
          "courseCover": "courseCover/courseCover预防感冒1547003167675.png",
          "parentId": 236
        }
      ],
      "sectionId": 236,
      "sectionLevel": "2",
      "parentId": 207,
      "sectionName": "day2",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
    }
  ],
  "sectionId": 207,
  "sectionLevel": "1",
  "parentId": 0,
  "content": "undefined",
  "sectionName": "治疗",
  "courseDrillId": 10,
  "userSectionStats": 1,
  "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
}
];
var findTree = (list, teachers, acc=[])=>(list.forEach(item=>{
   teachers.includes(item.teacherName) && acc.push(item);
   findTree(item.childList ||[], teachers, acc);
}),acc);
var teachers = ['张三', '李四'];
findTree(list, teachers);

结果

[
  {
    "clockInState": 0,
    "sectionNum": "1",
    "courseBegin": "2019-02-09 00:00",
    "teacherName": "张三",
    "drillID": 0,
    "sectionId": 145,
    "sectionLevel": "3",
    "parentId": 131,
    "content": "章节看到了吗",
    "sectionName": "疾病类型",
    "courseDrillId": 10,
    "userSectionStats": 0,
    "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
  },
  {
    "sectionName": "吃西药",
    "clockInState": 0,
    "sectionNum": "1",
    "courseDrillId": 10,
    "courseBegin": "2019-02-23 00:00",
    "teacherName": "李四",
    "drillID": 0,
    "userSectionStats": 0,
    "sectionId": 402,
    "sectionLevel": "3",
    "courseCover": "courseCover/courseCover预防感冒1547003167675.png",
    "parentId": 236
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文