0

This is the object,

{
    "response": {
        "count": 49472,
        "items": [
            {
                "hello": "james",
                "title": "game",
                "player": "123",
                "home": "syd",

            }
        ]
    }
}

I would like to get a specific value from player and return 123 in the object using javascript. I have tried looking at other posts but I can't get it to work for my case.

I have managed to do this in python using the code below but it's not as simple to me with js.

for items in obj['response']['items']:
        print(items['player'])

4 Answers 4

1

    var myObject = {
    "response": {
        "count": 49472,
        "items": [
            {
                "hello": "james",
                "title": "game",
                "player": "123",
                "home": "syd"
            }
        ]
    }
};

myObject.response.items.forEach((v) => {
  console.log(v.player)
});
 // or
 
 myObject.response.items.map(res => console.log(res.player))
 
  // or
  
   console.log(myObject.response.items[0].player)
   //or
   
   console.log(myObject.response.items[0]['player'])

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

1 Comment

hey just wondering is it possible to turn the forEach code into a for loop doing the same thing. Just so its easier to use the value later.
1
var obj = var t = {
    "response": {
        "count": 49472,
        "items": [
            {
                "hello": "james",
                "title": "game",
                "player": "123",
                "home": "syd",

            }
        ]
    }
}

t.response.items[0].player
"123"

1 Comment

Thank you for the fast response
1

What you have is an object with an object which contains an array which contains an object. You can access it like so: myObject.response.items[0].player

(Run the code snippet to see the result)

var myObject = {
    "response": {
        "count": 49472,
        "items": [
            {
                "hello": "james",
                "title": "game",
                "player": "123",
                "home": "syd"
            }
        ]
    }
};

console.log(myObject.response.items[0].player);

1 Comment

Yes! Thank you I was trying different versions of this and I must have got something basic wrong.
0

Would be something like this:

response.items.forEach(item =>
    console.log(item.player)
)

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.