简体   繁体   中英

Is there any object property to extract the nested keys in javascript?

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 } }

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))

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();

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) 

假设x包含上面提到的对象,那么它将起作用:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM