简体   繁体   English

Javascript 如何过滤嵌套数组数据

[英]Javascript How to filter through nested array data

I have a search field which filters through an array when I'm typing.我有一个搜索字段,它在我打字时过滤一个数组。 So far it works ok, but I cannot search within the "courses" array!到目前为止它工作正常,但我无法在“课程”数组中搜索! How can I achieve this?我怎样才能做到这一点? The complete array looks like this:完整的数组如下所示:

const data = [{
  name: "john doe",
  city: "blabla",
  zipcode: "1234",
  email: "test@test.com",
  phone: "12345678",
  courses: [
   {
     title: "some course",
     provider: "some provider",
     end_date: "some date"
    },
   {
     title: "another course",
     provider: "another provider",
     end_date: "another date"
    },
  ]
]

Here is my JS code so far, where I can search through all fields, except the "courses"-array:到目前为止,这是我的 JS 代码,我可以在其中搜索所有字段,但“课程”数组除外:

data = data.filter(row => {
   return Object.keys(row).some(key => {
     return (
       String(row[key])
        .toLowerCase()
        .indexOf(filter) > -1
      );
   });
});

Can someone help me out?有人可以帮我吗?

The following will search both, all fields of your top level objects in data as well as all fields in the objects of each course array:以下将搜索data顶级对象的所有字段以及每个course数组的对象中的所有字段:

 const data = [{ name: "john doe", city: "blabla", zipcode: "1234", email: "test@test.com", phone: "12345678", courses: [ { title: "some course", provider: "some provider", end_date: "some date" }, { title: "another course", provider: "another provider", end_date: "another date" }, ]}, { name: "jane aire", city: "dooodaaah", zipcode: "987", email: "missjane@ladyjane.com", phone: "8997652", courses: [ { title: "how to win", provider: "by herself", end_date: "tomorrow" }, { title: "you can do it", provider: "Harry", end_date: "next week" }, ]} ]; // check whether `filter` is found in `value`: function chk(filter){return function(value){return String(value).toLowerCase().indexOf(filter)>-1}} const res=document.getElementById('res'); // show found results in <pre id="res"> // whenever the input changes: document.querySelector('input').oninput=ev=>{ let chkfilt=chk(ev.target.value || null); res.innerHTML=JSON.stringify( data.filter(row => { return Object.values(row).some(chkfilt) || row.courses.some(o=>Object.values(o).some(chkfilt)) }) ,null,2); }
 .as-console-wrapper {max-height:100% !important}
 <input type="text" placeholder="search string ..."> <pre id="res"></pre>

Below snippet could help you下面的片段可以帮助你

 const data = [ { name: "john doe", city: "blabla", zipcode: "1234", email: "test@test.com", phone: "12345678", courses: [ { title: "some course", provider: "some provider", end_date: "some date", }, { title: "another course", provider: "another provider", end_date: "another date", }, ], }, ] const courseTitle = "another" const res = data.filter( (el) => el.courses.find((course) => course.title.toLowerCase().includes(courseTitle.toLowerCase()) ) !== undefined ) console.log(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM