简体   繁体   English

Javascript 过滤器对象数组

[英]Javascript filter objects array

I need to obtain the Modules, with those postings whose locality equals to "Vienna", I am not being able to eliminate those that do not pass the validation.我需要获取模块,那些位置等于“维也纳”的帖子,我无法消除那些没有通过验证的帖子。

This is the json.这是 json。

const json = [
   {
      "title":"Module A",
      "postings":[
         {"name": "1", "categories":{"location":"Viena"},},
         {"name": "2", "categories":{"location":"Paris"},},
         {"name": "3", "categories":{"location":"Viena"},},
        ]
   },
   {
      "title":"Module B",
      "postings":[
         {"name": "1", "categories":{"location":"Madrid"},},
         {"name": "3", "categories":{"location":"Paris"},},
        ]
   },
   {
      "title":"Module C",
      "postings":[
         {"name": "1", "categories":{"location":"Madrid"},},
         {"name": "2", "categories":{"location":"Viena"},},
        ]
   },  
];

Expected result预期结果

[
  {
      "title":"Module A",
      "postings":[
         {"name": "1", "categories":{"location":"Viena"},},
         {"name": "3", "categories":{"location":"Viena"},},
        ]
   },
   {
      "title":"Module C",
      "postings":[
         {"name": "2", "categories":{"location":"Viena"},},
        ]
   },  
];

This is what I'm trying这就是我正在尝试的

const result = json
.filter(module => module.postings
.filter(posting => posting.categories.location === 'Viena').length);

Actual Result实际结果

I am not being able to remove those postings that do not meet the condition.我无法删除那些不符合条件的帖子。

filter needs to return a truthy value - so you should use some inside it not another filter filter需要返回一个真实的值 - 所以你应该在里面使用some而不是另一个filter

 const json = [ { "title":"Module A", "postings":[ {"name": "1", "categories":{"location":"Viena"},}, {"name": "2", "categories":{"location":"Paris"},}, {"name": "3", "categories":{"location":"Viena"},}, ] }, { "title":"Module B", "postings":[ {"name": "1", "categories":{"location":"Madrid"},}, {"name": "3", "categories":{"location":"Paris"},}, ] }, { "title":"Module C", "postings":[ {"name": "1", "categories":{"location":"Madrid"},}, {"name": "2", "categories":{"location":"Viena"},}, ] }, ]; var result = json.filter(x => x.postings.some(p => p.categories.location == "Viena")); console.log(result);

If you then want to also filter the postings it's a little bit more work如果您还想过滤postings ,则需要做更多的工作

 const json = [ { "title":"Module A", "postings":[ {"name": "1", "categories":{"location":"Viena"},}, {"name": "2", "categories":{"location":"Paris"},}, {"name": "3", "categories":{"location":"Viena"},}, ] }, { "title":"Module B", "postings":[ {"name": "1", "categories":{"location":"Madrid"},}, {"name": "3", "categories":{"location":"Paris"},}, ] }, { "title":"Module C", "postings":[ {"name": "1", "categories":{"location":"Madrid"},}, {"name": "2", "categories":{"location":"Viena"},}, ] }, ]; const filterLocation = loc => p => p.categories.location == loc; const isViena = filterLocation("Viena"); const result = json.filter(x => x.postings.some(isViena)).map(x => ({...x, postings: x.postings.filter(isViena) })); console.log(result);

I found a quick solution with the help of the reduce function.我在reduce function 的帮助下找到了一个快速的解决方案。

 const json = [ { "title":"Module A", "postings":[ {"name": "1", "categories":{"location":"Viena"},}, {"name": "2", "categories":{"location":"Paris"},}, {"name": "3", "categories":{"location":"Viena"},}, ] }, { "title":"Module B", "postings":[ {"name": "1", "categories":{"location":"Madrid"},}, {"name": "3", "categories":{"location":"Paris"},}, ] }, { "title":"Module C", "postings":[ {"name": "1", "categories":{"location":"Madrid"},}, {"name": "2", "categories":{"location":"Viena"},}, ] }, ]; let result = json.reduce((acc, curr) => { curr.postings = curr.postings.filter(e => e.categories.location == "Viena") if(curr.postings.length > 0) acc.push(curr); return acc; }, []); console.log(result);

If you want to learn more about that function you can find it here: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce如果您想了解有关 function 的更多信息,可以在这里找到: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

EDIT:编辑:

If you want to get the best performance you should consider using my solution, since with the help of reduce you have a complexity of O(n) .如果您想获得最佳性能,您应该考虑使用我的解决方案,因为在reduce的帮助下,您的复杂度为O(n) With the solution what @Jamiec suggested you get a complexity of O(2n) since you filter and map over it again including the cost of memory since you are creating an additional array.使用@Jamiec 建议的解决方案,您将获得O(2n)的复杂性,因为您filtermap再次对其进行处理,包括 memory 的成本,因为您正在创建一个额外的阵列。

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

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