简体   繁体   English

Javascript用另一个元素数组过滤一个数组

[英]Javascript filter an array with another array of elements

I want to implement a tag filter for a search and get an array of entries (variable: entries) like this with many entries: 我想为搜索实现标签过滤器,并获得具有许多条目的条目数组(变量:条目):

"entries":[
      {
         "id":1,
         "details":"text",
         "tags":[
            {
               "id":9,
               "label":"Label9"
            },
            {
               "id":6,
               "label":"Label6"
            },
         ],
         "date":"Mar 8, 2018 2:45:30 PM"
      }]

I want to filter this array with another array of tags (variable: tags) like this: 我想用另一个标签数组(变量:标签)过滤该数组,如下所示:

"tags":[
            {
               "id":6,
               "label":"Label6"
            }

At the end I need an array of those entries which contain all tags in the tags array. 最后,我需要一个包含条目的数组,这些条目包含标签数组中的所有标签。 ] ]

I wrote this code but something is wrong. 我写了这段代码,但是出了点问题。 It compares the id of each tag. 它比较每个标签的ID。

 const entries = [{ "id": 1, "details": "text", "tags": [{ "id": 9, "label": "Label9" }, { "id": 6, "label": "Label6" }, ], "date": "Mar 8, 2018 2:45:30 PM" }] const tags = [{ "id": 6, "label": "Label6" }] function containSearchTag(tags) { return entries.filter(function(el) { for (let i = 0; i < el.tags.length; i++) { for (let j = 0; j < tags.length; j++) { return el.tags[i].id === tags[j].id; } } }); } console.log(containSearchTag(tags)); 

You could use a Set and filter the array by checking the tags array with the set. 您可以使用Set并通过检查具有该set的tags数组来过滤该数组。

 var entries = [{ id: 1, details: "text", tags: [{ id: 9, label: "Label9" }, { id: 6, label: "Label6" }], date: "Mar 8, 2018 2:45:30 PM" }], tags = [{ id: 6, label: "Label6" }], ids = new Set(tags.map(({ id }) => id)), result = entries.filter(({ tags }) => tags.some(({ id }) => ids.has(id))); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You can use of Array.some to make it easier; 您可以使用Array.some使其变得更容易; like : 喜欢 :

 const entries = [{ id: 1, details: 'text', tags: [{ id: 9, label: 'Label9' }, { id: 6, label: 'Label6', }, ], date: 'Mar 8, 2018 2:45:30 PM', }, { id: 1, details: 'text', tags: [{ id: 9, label: 'Label9', }, { id: 12, label: 'Label12', }, ], date: 'Mar 8, 2018 2:45:30 PM', }]; function filterByTag(tags) { return entries.filter(x => x.tags.some(y => tags.some(z => z.id === y.id))); } console.log(filterByTag([{ id: 6, label: 'Label6', }]).length); console.log(filterByTag([{ id: 9, label: 'Label6', }]).length); console.log(filterByTag([{ id: 17, label: 'Label6', }]).length); 

Use Array.filter , Array.every and Array.some 使用Array.filterArray.everyArray.some

 let entries = [{"id":1,"details":"text","tags":[{"id":9,"label":"Label9"},{"id":6,"label":"Label6"},],"date":"Mar 8, 2018 2:45:30 PM"}]; let tag = [{"id":6,"label":"Label6"}]; // Create an array of unique id's let ids = tag.map(({id}) => id); // filter only those entries which have all the tag ids specified in tag array let result = entries.filter(({tags}) => ids.every((id) => tags.some((tag) => tag.id === id))); console.log(result); 

Try This I hope it will help you 试试这个,希望对您有帮助

 var data = JSON.parse('{"entries":[{"id":1,"details":"text","tags":[{"id":9,"label":"Label9"},{"id":6,"label":"Label6"}],"date":"Mar 8, 2018 2:45:30 PM"}]}'); let tag = JSON.parse('[{"id":6,"label":"Label6"}]'); data.entries.forEach((element)=>{ element.tags.forEach((value)=>{ tag.forEach((find)=>{ if(find.id === value.id) { console.log("tag is avaliable "); console.log(value); } }); }); }); 

An optimized solution can be to create a map of all the ids of tags present corresponding to each entry and than search for all the tag ids of the tag array. 一种优化的解决方案可以是创建一个与每个条目相对应的所有标签id的映射,然后搜索标签数组的所有标签id。 Search in map can be done in O(1) time. 可以在O(1)时间内完成地图搜索。 which can help in reducing the overall time complexity. 这可以帮助降低整体时间复杂度。

 var entries = [{"id":1,"details":"text","tags":[{"id":9,"label":"Label9"},{"id":6,"label":"Label6"}],"date":"Mar 8, 2018 2:45:30 PM"}]; var tags =[{"id":6, "label":"Label6"}]; var result = entries.filter((obj) => { var tagMap = obj.tags.reduce((a, o) =>{ a[o.id] = true; return a;}, {}); var bool = true; tags.forEach((o) => {var x = tagMap[o.id] ? true : false; bool = bool && x;}) return bool; }); console.log(result); 

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

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