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

时间:2018-06-13 09:39:04

标签: javascript arrays filter

我想为搜索实现一个标记过滤器,并获得一个包含许多条目的条目数组(变量:条目):

"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"
            }

最后,我需要一个包含tags数组中所有标签的条目数组。 ]

我写了这段代码,但有些不对劲。它会比较每个标签的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));

5 个答案:

答案 0 :(得分:0)

您可以使用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; }

答案 1 :(得分:0)

您可以使用Array.some来简化操作;喜欢:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用Array.filterArray.everyArray.some

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

试试这个我希望它能帮到你

button {
    cursor: inherit;
}

答案 4 :(得分:0)

优化的解决方案可以是创建对应于每个条目的所有标签ID的映射,并且搜索标签阵列的所有标签ID。在地图中搜索可以在O(1)时间内完成。这可以帮助减少总体时间复杂度。

&#13;
&#13;
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);
&#13;
&#13;
&#13;