【问题标题】:How to get the first object of an array by condition?如何按条件获取数组的第一个对象?
【发布时间】:2021-08-02 17:07:42
【问题描述】:

我有一个回应: 响应正文:

[
    {
        "id": "6094f8253e2bf70001827add",
        "name": "groupdk2502",
        "type": "One",
        
    },
    {
        "id": "5f7ae257c64bb7000168175a",
        "name": "Badminton Vejle",
        "type": "Group",
        
    },
    {
        "id": "5f76c43cec5fdeb57baa0c31",
        "name": "Nl-golf Vejle",
        "type": "Group",
        
    },
    {
        "id": "5f7ae258c64bb7000168176f",
        "name": "Schaatsen Vejle",
        "type": "Group",
        
    },
    {
        "id": "5f7ae258c64bb70001681775",
        "name": "Bridge Vejle",
        "type": "Group",
        
    }
]

我收到一个包含 5 个对象的数组。我想为“type”中的第一个对象设置变量:“Group”。 attachment image。 但我不知道路。有人帮我吗???

【问题讨论】:

  • 为什么不使用.fitler
  • set the variable for the first object within "type": "Group".是什么意思
  • @evolutionxbox .filter 需要遍历整个数组,如果没有必要
  • @Hive7 当然可以,但.find 也是如此? (直到至少找到匹配项)
  • Find 一旦找到第一个匹配项就会退出,.filter 不会。过滤器也会创建一个新列表@evolutionxbox

标签: javascript arrays object postman


【解决方案1】:

您要查找的是.find,它返回匹配谓词或未定义的第一个匹配项。例如:

data.find(x => x.type === "Group");

请看下面的例子

const data = [
    {
        "id": "6094f8253e2bf70001827add",
        "name": "groupdk2502",
        "type": "One",
        
    },
    {
        "id": "5f7ae257c64bb7000168175a",
        "name": "Badminton Vejle",
        "type": "Group",
        
    },
    {
        "id": "5f76c43cec5fdeb57baa0c31",
        "name": "Nl-golf Vejle",
        "type": "Group",
        
    },
    {
        "id": "5f7ae258c64bb7000168176f",
        "name": "Schaatsen Vejle",
        "type": "Group",
        
    },
    {
        "id": "5f7ae258c64bb70001681775",
        "name": "Bridge Vejle",
        "type": "Group",
        
    }
];

const first = data.find(x => x.type === "Group");
console.log(first);

【讨论】:

    【解决方案2】:

    find可以直接找到第一个元素。

    const arr = [
      {
        id: "6094f8253e2bf70001827add",
        name: "groupdk2502",
        type: "One",
      },
      {
        id: "5f7ae257c64bb7000168175a",
        name: "Badminton Vejle",
        type: "Group",
      },
      {
        id: "5f76c43cec5fdeb57baa0c31",
        name: "Nl-golf Vejle",
        type: "Group",
      },
      {
        id: "5f7ae258c64bb7000168176f",
        name: "Schaatsen Vejle",
        type: "Group",
      },
      {
        id: "5f7ae258c64bb70001681775",
        name: "Bridge Vejle",
        type: "Group",
      },
    ];
    
    const first = arr.find((o) => o.type === "Group");
    console.log(first);

    同样使用filter,然后找到result[0]

    const arr = [
      {
        id: "6094f8253e2bf70001827add",
        name: "groupdk2502",
        type: "One",
      },
      {
        id: "5f7ae257c64bb7000168175a",
        name: "Badminton Vejle",
        type: "Group",
      },
      {
        id: "5f76c43cec5fdeb57baa0c31",
        name: "Nl-golf Vejle",
        type: "Group",
      },
      {
        id: "5f7ae258c64bb7000168176f",
        name: "Schaatsen Vejle",
        type: "Group",
      },
      {
        id: "5f7ae258c64bb70001681775",
        name: "Bridge Vejle",
        type: "Group",
      },
    ];
    
    const result = arr.filter((o) => o.type === "Group");
    const first = result[0];
    console.log(first);

    【讨论】:

    • 这不只是.find 效率低得多的版本吗?
    • @Hive7 两个答案都已更新。它只是首先出现在我的脑海中。
    • @Hive7 这只是另一种方式,但根本没有错。
    【解决方案3】:

    let res = [
        {
            "id": "6094f8253e2bf70001827add",
            "name": "groupdk2502",
            "type": "One",
            
        },
        {
            "id": "5f7ae257c64bb7000168175a",
            "name": "Badminton Vejle",
            "type": "Group",
            
        },
        {
            "id": "5f76c43cec5fdeb57baa0c31",
            "name": "Nl-golf Vejle",
            "type": "Group",
            
        },
        {
            "id": "5f7ae258c64bb7000168176f",
            "name": "Schaatsen Vejle",
            "type": "Group",
            
        },
        {
            "id": "5f7ae258c64bb70001681775",
            "name": "Bridge Vejle",
            "type": "Group",
            
        }
    ]
    
    let ans = res.find((f) => f.type === "Group")
    console.log(ans)

    【讨论】:

      【解决方案4】:

      这应该可以正常工作:

          let res = [
            {
                "id": "6094f8253e2bf70001827add",
                "name": "groupdk2502",
                "type": "One",
                
            },
            {
                "id": "5f7ae257c64bb7000168175a",
                "name": "Badminton Vejle",
                "type": "Group",
                
            },
            {
                "id": "5f76c43cec5fdeb57baa0c31",
                "name": "Nl-golf Vejle",
                "type": "Group",
                
            },
            {
                "id": "5f7ae258c64bb7000168176f",
                "name": "Schaatsen Vejle",
                "type": "Group",
                
            },
            {
                "id": "5f7ae258c6t56d700168174p",
                "name": "Some Name",
                "type": "One",
                
            },
            {
                "id": "5f7ae258c64bb70001681775",
                "name": "Bridge Vejle",
                "type": "Group",
                
            }
          ];
          
          let getFirstGroup = arr => {
            // loop over every object in the array
            for(let obj of arr){
              // if the type of current object (obj) is "Group", then return that object
              if(obj.type == "Group")
                return obj;
            }
            
            // else, return false
            return false;
          };
          
          let first = getFirstGroup(res);
          console.log(first); // prints out second object in the array
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-26
        • 2022-11-27
        • 2020-02-07
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        • 2021-11-04
        相关资源
        最近更新 更多