【问题标题】:Get list properties of object React Native获取对象 React Native 的列表属性
【发布时间】:2021-02-17 07:22:10
【问题描述】:

我不想获取数组中每个对象的 ID。
我有这个数组

[
  {
    "-ML8Lx8jOSv64Ecat611": {
      "category": "Automotive Parts & Accessories",
      "description": "I will be there at least but I don't know if I can help in any way I can",
      "item": "Apple Watch",
      "price": "429,99",
      "timestamp": "2020-11-02T21:28:11+07:00",
      "viewCount": 0
    }
  }
]

我期望得到的是

[
  {
      "category": "Automotive Parts & Accessories",
      "description": "I will be there at least but I don't know if I can help in any way I can",
      "item": "Apple Watch",
      "price": "429,99",
      "timestamp": "2020-11-02T21:28:11+07:00",
      "viewCount": 0
  }
]

【问题讨论】:

    标签: arrays json firebase react-native firebase-realtime-database


    【解决方案1】:
    [
      {
        "-ML8Lx8jOSv64Ecat611": {
          "category": "Automotive Parts & Accessories",
          "description": "I will be there at least but I don't know if I can help in any way I can",
          "item": "Apple Watch",
          "price": "429,99",
          "timestamp": "2020-11-02T21:28:11+07:00",
          "viewCount": 0
        }
      }
    ]
    

    在上面的示例中,您在一个需要唯一键的对象中使用了多个对象。您可以像这样简单地在数组中使用多个对象:

    [
        {
          "category": "Automotive Parts & Accessories",
          "description": "I will be there at least but I don't know if I can help in any way I can",
          "item": "Apple Watch",
          "price": "429,99",
          "timestamp": "2020-11-02T21:28:11+07:00",
          "viewCount": 0
        },
        {
          "category": "Automotive Parts & Accessories",
          "description": "I will be there at least but I don't know if I can help in any way I can",
          "item": "Apple Watch",
          "price": "429,99",
          "timestamp": "2020-11-02T21:28:11+07:00",
          "viewCount": 0
        },
        .....
    ]
    

    现在您可以通过数组索引访问每个对象。希望我的回答对你有帮助。

    【讨论】:

    • 不,这并不意味着我需要一些方法来削减对象的 ID。
    【解决方案2】:

    试试这个

    var data = [
      {
        "-ML8Lx8jOSv64Ecat611": {
          "category": "Automotive Parts & Accessories",
          "description": "I will be there at least but I don't know if I can help in any way I can",
          "item": "Apple Watch",
          "price": "429,99",
          "timestamp": "2020-11-02T21:28:11+07:00",
          "viewCount": 0
        }
      },
      {
        "-NOa8Lx8jOSv64Ecat611": {
          "category": "Automotive Parts & Accessories",
          "description": "I will be there at least but I don't know if I can help in any way I can",
          "item": "Apple Watch",
          "price": "429,99",
          "timestamp": "2020-11-02T21:28:11+07:00",
          "viewCount": 0
        }
      }
    ]
    
    console.log(data.map((item) => Object.values(item)[0]))

    【讨论】:

      【解决方案3】:

      由于您想要在输出中的对象是嵌套的,因此您需要在 JSON 中循环两个级别:

      let input = [
        {
          "-ML8Lx8jOSv64Ecat611": {
            "category": "Automotive Parts & Accessories",
            "description": "I will be there at least but I don't know if I can help in any way I can",
            "item": "Apple Watch",
            "price": "429,99",
            "timestamp": "2020-11-02T21:28:11+07:00",
            "viewCount": 0
          }
        }
      ];
      
      let result = []
      input.forEach(obj => {
        Object.keys(obj).forEach(key => {
          result.push(obj[key]);
        });
      });
      
      console.log(result);

      【讨论】:

        猜你喜欢
        • 2022-11-18
        • 1970-01-01
        • 1970-01-01
        • 2020-02-24
        • 1970-01-01
        • 2014-02-04
        • 2022-01-10
        • 2017-06-29
        • 1970-01-01
        相关资源
        最近更新 更多