【问题标题】:React native give object a name in arrayReact native 在数组中给对象一个名称
【发布时间】:2019-05-12 10:03:49
【问题描述】:

我的数组如下所示:

Array [
  Object {
    "color": "Brown",
    "time": "18:32",
  },
  Object {
    "color": "Red",
    "time": "18:33",
  },
]

但是如何将 Object 更改为名称?

我是这样推的

const itemLogs = [];

childSnapshot.forEach((csh) => {
let childKeys = csh.key;
itemLogs.push({
    color:   csh.val().color,
    time:   csh.val().time
});
});
this.setState({childData: itemLogs});

更新:

这就是我想要的方式:

10-05-2019 [
  14:27 {
    "color": "Brown",
    "time": "14:27",
  },
  14:23 {
    "color": "Red",
    "time": "14:23",
  },
],
11-06-2019 [
like above but other data
]

我希望这是一个更好的例子。

【问题讨论】:

    标签: arrays reactjs react-native object arraylist


    【解决方案1】:

    调试器将您的数据可视化如下:

    Array [
      Object {
        "color": "Brown",
        "time": "18:32",
      },
      Object {
        "color": "Red",
        "time": "18:33",
      },
    ]
    

    但实际上是这样的:

    [
      {
        "color": "Brown",
        "time": "18:32",
      },
      {
        "color": "Red",
        "time": "18:33",
      },
    ]
    

    您可以将name 属性添加到您的对象中:

    itemLogs.push({
        color:   csh.val().color,
        time:   csh.val().timem,
        name: 'YOUR_NAME_GOES_HERE'
    });
    

    那么您的数据将如下所示:

    [
      {
        "color": "Brown",
        "time": "18:32",
        "name": "YOUR_FIRST_NAME",
      },
      {
        "color": "Red",
        "time": "18:33",
        "name": "YOUR_SECOND_NAME",
      },
    ]
    

    更新的解决方案:

    const itemLogs = {}; //create an object
    
    var arr = []; // create a temporary array 
    var tmp = {}; //create a temporary object 
     tmp["14:27"] = {
        "color": "Brown",
        "time": "14:27",
      }; 
    
    arr.push(tmp); // add object to array 
    
    
    var tmp2 = {}; //create another tmp object
     tmp2["14:23"] = {
       "color": "Red",
        "time": "14:23",
      }; 
    arr.push(tmp2); // add object to array 
    itemLogs["10-05-2019"] = arr; // add the array to itemLogs with new key
    
    console.log(itemLogs);

    【讨论】:

    • 谢谢蒂姆,但我的数据库中有多个孩子,所以我希望它有多个层次。我将更新我的代码,以便您更好地理解它。
    • 谢谢!它现在工作得很好!我会在这里发布答案。
    • 你也知道如何显示数据吗?我正在尝试使用地图或平面列表,但无法正常工作。
    【解决方案2】:

    感谢蒂姆

    let userId = firebase.auth().currentUser.uid;
        firebase.database().ref('table/' + userId).on('value', (snapshot) => {
            const itemLogs = {};
            const tmp = {};
            snapshot.forEach((childSnapshot) => {
                childSnapshot.forEach((csh) => {
    
                    tmp[csh.key] = {
                        color:   csh.val().color,
                        time:   csh.val().time
                    };
                });
                itemLogs[childSnapshot.key] = tmp;
                console.log(itemLogs);
            });
        });
    

    现在看起来像这样:

        Object {
      "11-5-2019": Object {
        "18:32": Object {
          "color": "Brown",
          "time": "18:32",
        },
        "18:33": Object {
          "color": "Red",
          "time": "18:33",
        },
      },
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      相关资源
      最近更新 更多