0

I am trying to extract only the team names from this object of objects but not getting a way to do it

I have already tried Object.keys(data) but it is only showing the year of matches and I have tried Object.values(data) but its showing the output with both team name and value associated with it.

{ '2008': 
   { 'Chennai Super Kings': 9,
     'Delhi Daredevils': 7,
     'Royal Challengers Bangalore': 4,
     'Kolkata Knight Riders': 5,
     'Rajasthan Royals': 13,
     'Kings XI Punjab': 10,
     'Deccan Chargers': 2,
     'Mumbai Indians': 7 },
  '2009': 
   { 'Royal Challengers Bangalore': 9,
     'Delhi Daredevils': 10,
     'Deccan Chargers': 9,
     'Chennai Super Kings': 8,
     'Kolkata Knight Riders': 3,
     'Rajasthan Royals': 6,
     'Kings XI Punjab': 7,
     'Mumbai Indians': 4 },
  '2010': 
   { 'Mumbai Indians': 11,
     'Delhi Daredevils': 7,
     'Kolkata Knight Riders': 6,
     'Deccan Chargers': 8,
     'Royal Challengers Bangalore': 8,
     'Chennai Super Kings': 9,
     'Rajasthan Royals': 6,
     'Kings XI Punjab': 4 } }

4 Answers 4

3

If you dealing with vanilla JS (no lodash, underscore, etc), you can get the list of teams like that

var obj = {/* your object here */};

// for browsers that have flat() support
var onlyTeams = Object.values(obj).map(Object.keys).flat()

// for browsers without flat() support
var onlyTeams = Object.values(obj).map(Object.keys).reduce((acc, val) => acc.concat(val), []);

If you want to dedup them, you can do that like this

var dedupedTeams = Array.from(new Set(onlyTeams))
Sign up to request clarification or add additional context in comments.

3 Comments

Note: flat() isn't supported by Microsoft Edge
@GetOffMyLawn, then we can use reduce() instead. I will update my answer.
@GetOffMyLawn it is in the new chromium-based Edge.
1

Since the team names inside the nested objects, you should use Object.keys() inside map()

var obj = { '2008': 
   { 'Chennai Super Kings': 9,
     'Delhi Daredevils': 7,
     'Royal Challengers Bangalore': 4,
     'Kolkata Knight Riders': 5,
     'Rajasthan Royals': 13,
     'Kings XI Punjab': 10,
     'Deccan Chargers': 2,
     'Mumbai Indians': 7 },
  '2009': 
   { 'Royal Challengers Bangalore': 9,
     'Delhi Daredevils': 10,
     'Deccan Chargers': 9,
     'Chennai Super Kings': 8,
     'Kolkata Knight Riders': 3,
     'Rajasthan Royals': 6,
     'Kings XI Punjab': 7,
     'Mumbai Indians': 4 },
  '2010': 
   { 'Mumbai Indians': 11,
     'Delhi Daredevils': 7,
     'Kolkata Knight Riders': 6,
     'Deccan Chargers': 8,
     'Royal Challengers Bangalore': 8,
     'Chennai Super Kings': 9,
     'Rajasthan Royals': 6,
     'Kings XI Punjab': 4 } }
     
 var teams = Object.values(obj).map(o => Object.keys(o));
 console.log(teams);

Please Note: If you the teams in single array you have to use flat().

var teams = Object.values(obj).map(o => Object.keys(o)).flat();

3 Comments

Hello Sir,Thanks for the solution but your code is showing the output of teams thrice ,i want the output to be shown only one time.And one more question ,is there no other way than using map.
@AdityaDhiman to remove duplicates use let teams = [...new Set(Object.values(obj).map(o => Object.keys(o)).flat())]
Note: flat() isn't supported by Microsoft Edge
0

Suppose x contains the object mentioned above then this will work :

Object.keys(x).map(e => Object.keys(x[e])).flat(); //tested

Comments

0

You can iterate over first-level objects and then their keys to get team names:

data = { '2008': { 'Chennai Super Kings': 9, 'Delhi Daredevils': 7, 'Royal Challengers Bangalore': 4, 'Kolkata Knight Riders': 5, 'Rajasthan Royals': 13, 'Kings XI Punjab': 10, 'Deccan Chargers': 2, 'Mumbai Indians': 7 }, '2009': { 'Royal Challengers Bangalore': 9, 'Delhi Daredevils': 10, 'Deccan Chargers': 9, 'Chennai Super Kings': 8, 'Kolkata Knight Riders': 3, 'Rajasthan Royals': 6, 'Kings XI Punjab': 7, 'Mumbai Indians': 4 }, '2010': { 'Mumbai Indians': 11, 'Delhi Daredevils': 7, 'Kolkata Knight Riders': 6, 'Deccan Chargers': 8, 'Royal Challengers Bangalore': 8, 'Chennai Super Kings': 9, 'Rajasthan Royals': 6, 'Kings XI Punjab': 4 } }

Object.values(data).forEach(value => {
    Object.keys(value).forEach(token => console.log(token));
});

In case you want to get team names for each year separately, do this:

data = { '2008': { 'Chennai Super Kings': 9, 'Delhi Daredevils': 7, 'Royal Challengers Bangalore': 4, 'Kolkata Knight Riders': 5, 'Rajasthan Royals': 13, 'Kings XI Punjab': 10, 'Deccan Chargers': 2, 'Mumbai Indians': 7 }, '2009': { 'Royal Challengers Bangalore': 9, 'Delhi Daredevils': 10, 'Deccan Chargers': 9, 'Chennai Super Kings': 8, 'Kolkata Knight Riders': 3, 'Rajasthan Royals': 6, 'Kings XI Punjab': 7, 'Mumbai Indians': 4 }, '2010': { 'Mumbai Indians': 11, 'Delhi Daredevils': 7, 'Kolkata Knight Riders': 6, 'Deccan Chargers': 8, 'Royal Challengers Bangalore': 8, 'Chennai Super Kings': 9, 'Rajasthan Royals': 6, 'Kings XI Punjab': 4 } }

Object.values(data).forEach(value => {
    console.log(Object.keys(value));
});

To get unique team names across the years do this:

data = { '2008': { 'Chennai Super Kings': 9, 'Delhi Daredevils': 7, 'Royal Challengers Bangalore': 4, 'Kolkata Knight Riders': 5, 'Rajasthan Royals': 13, 'Kings XI Punjab': 10, 'Deccan Chargers': 2, 'Mumbai Indians': 7 }, '2009': { 'Royal Challengers Bangalore': 9, 'Delhi Daredevils': 10, 'Deccan Chargers': 9, 'Chennai Super Kings': 8, 'Kolkata Knight Riders': 3, 'Rajasthan Royals': 6, 'Kings XI Punjab': 7, 'Mumbai Indians': 4 }, '2010': { 'Mumbai Indians': 11, 'Delhi Daredevils': 7, 'Kolkata Knight Riders': 6, 'Deccan Chargers': 8, 'Royal Challengers Bangalore': 8, 'Chennai Super Kings': 9, 'Rajasthan Royals': 6, 'Kings XI Punjab': 4 } }
var teams = new Set();
Object.values(data).forEach(value => Object.keys(value).forEach(team => teams.add(team)));
console.log(teams)

3 Comments

But i want the team names only one time not ithrice,cause i want to use them in highcharts to display them
Are the teams the same every year? Then take just one. If not then you can take unique team names considering all years. Confirm this.
Yes they are same every year

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.