Dekirai
Dekirai

Reputation: 13

Don't have an idea how to read this json format

I never had troubles to read a json from a url or anything that's builded like this:

{ success: true,
  name: 'Dekirai',
  clan: null,
  level: 7,
  exp: 3193,
  playtime: 0,
  tdrate: 0,
  kdrate: 0,
  matches_played: 0,
  matches_won: 0,
  matches_lost: 0,
  last_online: '2018-03-02T23:00:00.000Z',
  views: 1,
  favorites: 0,
  fame: 0,
  hate: 0 }

but the json I now have uses square brackets inside like this one here:

[ { id: 23,
    name: 'AeriaGames Login',
    player_limit: -1,
    player_online: -1,
    state: 2,
    last_update: '2018-02-14T16:22:28.000Z' },
  { id: 1,
    name: 'Auth',
    player_limit: -1,
    player_online: -1,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z' },
  { id: 10,
    name: 'English 1 (Europe)',
    player_limit: 4000,
    player_online: 871,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z' },
  { id: 11,
    name: 'English 2 (Europe)',
    player_limit: 4000,
    player_online: 48,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z' },
  { id: 12,
    name: 'English 3 (North America)',
    player_limit: 4000,
    player_online: 54,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z' } ]

My question now is:

How do I read the "player_online" value from "id: 10"?

Upvotes: 0

Views: 54

Answers (3)

brk
brk

Reputation: 50291

You can use filter method. This will return an array of matched property. Then use dot operator to get the property value

var x = [{
    id: 23,
    name: 'AeriaGames Login',
    player_limit: -1,
    player_online: -1,
    state: 2,
    last_update: '2018-02-14T16:22:28.000Z'
  },
  {
    id: 1,
    name: 'Auth',
    player_limit: -1,
    player_online: -1,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z'
  },
  {
    id: 10,
    name: 'English 1 (Europe)',
    player_limit: 4000,
    player_online: 871,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z'
  },
  {
    id: 11,
    name: 'English 2 (Europe)',
    player_limit: 4000,
    player_online: 48,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z'
  },
  {
    id: 12,
    name: 'English 3 (North America)',
    player_limit: 4000,
    player_online: 54,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z'
  }
]

var getPlayerOnline = x.filter(function(item) {
  return item.id === 10 // will return object where id is 10

})[0].player_online;

console.log(getPlayerOnline)

Upvotes: 0

Pulkit Aggarwal
Pulkit Aggarwal

Reputation: 2672

You can use find for finding the value of player_online.

var data = [{
    id: 23,
    name: 'AeriaGames Login',
    player_limit: -1,
    player_online: -1,
    state: 2,
    last_update: '2018-02-14T16:22:28.000Z'
  },
  {
    id: 1,
    name: 'Auth',
    player_limit: -1,
    player_online: -1,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z'
  },
  {
    id: 10,
    name: 'English 1 (Europe)',
    player_limit: 4000,
    player_online: 871,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z'
  },
  {
    id: 11,
    name: 'English 2 (Europe)',
    player_limit: 4000,
    player_online: 48,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z'
  },
  {
    id: 12,
    name: 'English 3 (North America)',
    player_limit: 4000,
    player_online: 54,
    state: 2,
    last_update: '2018-03-03T14:17:22.000Z'
  }
]

var getPlayerOnline = data.find(obj => obj.id === 12).player_online

console.log(getPlayerOnline)

Upvotes: 0

vityavv
vityavv

Reputation: 1490

let player_online = yourObject.find(el => el.id === 10).player_online

Upvotes: 3

Related Questions