【问题标题】:How map and filter over array of objects?如何映射和过滤对象数组?
【发布时间】:2019-09-29 03:03:28
【问题描述】:

对不起,伙计们,但我有一个愚蠢的问题.. 我需要从 json 响应中迭代对象,并且只获取那些满足某些条件的对象。 回复是这样的:

  result = [
      {
        "type": "EVENT",
        "id": "001",
        "tags": [
            {
            "id": "98765",
            "name": "home"
            }
        ]
      },
      {
        "type": "EVENT",
        "id": "002",
        "tags": [
            {
            "id": "7654",
            "name": "contact"
            }
        ]
      },
      {
        "type": "EVENT",
        "id": "003",
        "tags": []
      }
    ]

我只需要使用类型为“事件”且标签中的名称属性为 home 的那些。

我尝试了映射和过滤,但没有得到想要的结果

const eventType = result.filter(type => type.type == 'EVENT')
 const nameFilter = 
    eventType.map(item => item.tags)
    .filter(sub => sub.length) // remove empty []
    .map(subarray => subarray.map(element =>  element.name )
    .filter(element => element == 'home')); 

结果:

 [
  ['home'], // dosen t work for me, because need all the object
  [],
  []
 ]

【问题讨论】:

    标签: javascript arrays object ecmascript-6 functional-programming


    【解决方案1】:

    您可以使用filter 循环遍历数组。使用some 检查tags 数组中的至少一个元素是否具有属性name,其值为"home"

    result.filter(o => o.type === "EVENT" && o.tags.some(s => s.name === "home"));
    

    演示:

    let result = [{"type":"EVENT","id":"001","tags":[{"id":"98765","name":"home"}]},{"type":"EVENT","id":"002","tags":[{"id":"7654","name":"contact"}]},{"type":"EVENT","id":"003","tags":[]}];
    
    let filtered = result.filter(o => o.type === "EVENT" && o.tags.some(s => s.name === "home"));
    
    console.log( filtered );

    【讨论】:

      【解决方案2】:

      您只需要filter() 并使用find()some() 来检查标签中存在的名称。

      var filter = result.filter(c=> c.type == "EVENT" && c.tags.find(d=>d.name == 'home'));
      var filter = result.filter(c=> c.type == "EVENT" && c.tags.some(d=>d.name == 'home'));
      

      let result=  [
            {
              "type": "EVENT",
              "id": "001",
              "tags": [
                  {
                  "id": "98765",
                  "name": "home"
                  }
              ]
            },
            {
              "type": "EVENT",
              "id": "002",
              "tags": [
                  {
                  "id": "7654",
                  "name": "contact"
                  }
              ]
            },
            {
              "type": "EVENT",
              "id": "003",
              "tags": []
            }
          ]
          
          var filter = result.filter(c=> c.type == "EVENT" && c.tags.find(d=>d.name == 'home'));
          
          console.log(filter);

      【讨论】:

        【解决方案3】:

        如果您只是想从根数组中选择元素,那么 filter 是合适的方法。 如果您还想转换结果(例如,选择 TAG 对象),您可以使用 flatMap

        result.flatMap( e => (e.type == 'EVENT' && e.tags.some(t => t.name==='home')) ? e.tags : [] )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-07-25
          • 2022-01-14
          • 2021-04-19
          • 2020-11-12
          • 1970-01-01
          • 2020-06-27
          • 2022-01-25
          • 2020-11-23
          相关资源
          最近更新 更多