【问题标题】:Getting the object from an array on the basis of id [duplicate]根据id [重复]从数组中获取对象
【发布时间】:2023-01-22 22:52:37
【问题描述】:

我有一个数组如下:

data = [
  {
    "data": {
        "id":1,
        "vol":"0.0"
        "requiredId":100
        "details":{
            "ABC":"8.30",
            "OFG":"13.85",
            "SPG":"70.80"
        }
        
    }
  },
   {
    "data": {
        "id":2,
        "vol":"1.0",
        "requiredId":2
        "details":{
            "ABC":"3.30",
            "OFG":"15.85",
            "SPG":"70.80"
        }
        
    }
  }
]

我只想从此数组中获取 id 等于 requiredId 的对象。我怎样才能做到这一点?

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:
    data.find(item => item.data.id === item.data.requiredId)
    

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 2018-04-13
      • 2020-02-20
      • 1970-01-01
      • 2021-06-30
      • 2019-06-28
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多