1

{"-L0bFExUeZXB3-MUXCda":{"Comment":"GOOD","Date":"18 December","User":"OlaNord"}}

{"-L0bFCJh5SPUOWMjTRKu":{"Comment":"ok","Date":"18 December","User":"OlaNord"}}

{"-L0bFA2uzsGDizxxzN1p":{"Comment":"wewwe","Date":"18 December","User":"OlaNord"}}

I have an array of some objects inside. I need to access different values.When I try to retrieve the key name, I get undefined.

Here's my code:

// Path for selected category
var categoryRef = firebase.database().ref("forum/" + currentCategory);

categoryRef.once("value", function(snapshot) {
    firebase.auth().onAuthStateChanged(user => {
        var key = user.uid;
        var postComments = [];

        for (var key in snapshot.val()) {
            var comments = snapshot.val()[key]['comments'];
            postComments.push(comments);
        }
    })
})

enter image description here

9
  • 1
    can you post the json that comes in the snapshot, not the picture of it, it is hard to understand Commented Dec 18, 2017 at 5:06
  • 1
    What is being logged? Snapshot? Commented Dec 18, 2017 at 5:08
  • the picture is the array after the value is pushed. Commented Dec 18, 2017 at 5:08
  • @SanderHellesø can you just stringify and post the array rather? console.log(JSON.stringify(snapshot.val())); Commented Dec 18, 2017 at 5:10
  • added the json now Commented Dec 18, 2017 at 5:15

3 Answers 3

1

You can try accessing the Comment as below, if the array you have posted in question can be iterated as below:

var data = [{
        "-L0bFExUeZXB3-MUXCda": {
            "Comment": "GOOD",
            "Date": "18 December",
            "User": "OlaNord"
        }
    },

    {
        "-L0bFCJh5SPUOWMjTRKu": {
            "Comment": "ok",
            "Date": "18 December",
            "User": "OlaNord"
        }
    },

    {
        "-L0bFA2uzsGDizxxzN1p": {
            "Comment": "wewwe",
            "Date": "18 December",
            "User": "OlaNord"
        }
    }
]

var postComments = [];
for (var key of data) {
    for (var values in key) {
        console.log(key[values].Comment) //access the comment
        postComments.push(key[values].Comment);
    }
}

console.log(postComments);

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

Comments

0

// Path for selected category
var categoryRef = firebase.database().ref("forum/" + currentCategory);

categoryRef.once("value", function(snapshot) {
    firebase.auth().onAuthStateChanged(user => {
        var key = user.uid;
        var postComments = [];

        for (var key in snapshot.val()) {
            var comments = snapshot.val()[key]['Comment'];
             //CHange key Name
            postComments.push(comments);
        }
    })
})

Comments

0

You can use Object.values to access the values inside a key.

var data = [{"-L0bFExUeZXB3-MUXCda":{"Comment":"GOOD","Date":"18 December","User":"OlaNord"}},{"-L0bFCJh5SPUOWMjTRKu":{"Comment":"ok","Date":"18 December","User":"OlaNord"}},{"-L0bFA2uzsGDizxxzN1p":{"Comment":"wewwe","Date":"18 December","User":"OlaNord"}}];
var result = data.reduce((r,o) => r.concat(Object.values(o).map(({Comment}) => Comment)),[]);
console.log(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.