Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get key value of dictionary

How do I get the value from a javascript dictionary? (I don't even know if it's called a dictionary in javascript)

I get the following object (friends) from the facebook sdk. How do I, for example, loop through the names?

{data: [{id: "xxxxx", name: "Friend name"}, {id: "xxxxx", name: "Friend name"}]}
like image 210
user984003 Avatar asked Oct 28 '25 11:10

user984003


1 Answers

In JavaScript dictionaries are objects. To access object properties you may use either dot notation or square brackets notation. To iterate an array you may use simple for loop.

var obj = {
        data: [{
            id: "xxxxx",
            name: "Friend name"
        }, {
            id: "xxxxx",
            name: "Friend name"
        }]
    };

for (var i = 0, len = obj.data.length; i < len; i++) {
    console.log(obj.data[i].name);
}
like image 156
VisioN Avatar answered Oct 31 '25 01:10

VisioN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!