0

My array looks like this:

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

But how can I change Object to a name?

I push it like this

const itemLogs = [];

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

UPDATE:

This is how I would like to have it:

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
]

I hope this is an better Example.

2 Answers 2

1

The debugger visualizes your data like this:

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

But it actually looks like this:

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

You can add a name property to your objects with:

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

Then your data would look like this:

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

Updated solution:

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);

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Tim, but I have multiple childs in my database so I would love to have it in multiple layers. I will update my code so you will understand it better.
Thank you! It works perfectly fine now! I will post the answere here.
do you also know how to show the data? I'm trying to use map or flatlist but I can't get it working.
0

Thanks to Tim

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);
        });
    });

Now it looks like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.