Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

javascript - How can I map through the given object to get "value"..?

    {
    "confirmed": {
        "value": 104854731,
        "detail": "https://covid19.mathdro.id/api/confirmed"
    },
    "recovered": {
        "value": 58294751,
        "detail": "https://covid19.mathdro.id/api/recovered"
    },
    "deaths": {
        "value": 2282535,
        "detail": "https://covid19.mathdro.id/api/deaths"
    },
    "dailySummary": "https://covid19.mathdro.id/api/daily",
    "dailyTimeSeries": {
        "pattern": "https://covid19.mathdro.id/api/daily/[dateString]",
        "example": "https://covid19.mathdro.id/api/daily/2-14-2020"
    },
    "image": "https://covid19.mathdro.id/api/og",
    "source": "https://github.com/mathdroid/covid19",
    "countries": "https://covid19.mathdro.id/api/countries",
    "countryDetail": {
        "pattern": "https://covid19.mathdro.id/api/countries/[country]",
        "example": "https://covid19.mathdro.id/api/countries/USA"
    },
    "lastUpdate": "2021-02-05T05:22:38.000Z"
    }
    function InfoPanel() {
  return (
    <div>
      <div className={classes.root}>
        <Grid container spacing={3}>
          <Grid item xs={12} sm={4}>
            <Paper className={classes.paper}>Infected</Paper>
          </Grid>
          <Grid item xs={12} sm={4}>
            <Paper className={classes.paper}>Recovered</Paper>
          </Grid>
          <Grid item xs={12} sm={4}>
            <Paper className={classes.paper}>Deaths</Paper>
          </Grid>
        </Grid>
      </div>
    </div>
  );
}

I want to display the "value" from "confirmed" key into the "Infected" parameter using map function or anything else. Is there any possible way of doing that or map function only works on arrays.

question from:https://stackoverflow.com/questions/66057999/how-can-i-map-through-the-given-object-to-get-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
const yourObj = {  
 "confirmed": {      
     "value": 123456789,
     "detail": "https://covid19.mathdro.id/api/confirmed"
 },
 "deaths": {      
     "value": 99999,
     "detail": "https://covid19.mathdro.id/api/confirmed"
 },
 "recovered": {      
     "value": 8888,
     "detail": "https://covid19.mathdro.id/api/confirmed"
 }
}

const desiredKeys = Object.keys(yourObj)
const stats = desiredKeys.reduce((finalResult, desiredKey) => {
    let temp = {}

    temp.category = desiredKey
    temp.value = yourObj[desiredKey].value
    
    finalResult.push(temp);
    
    return finalResult
}, [])

console.log(stats)   //[{category: "confirmed",  value: 123456789}, {  category: "deaths",  value: 9999}, {  category: "recovered",  value: 8888}]
//InfoPanel
function InfoPanel(){
 return (
   //more code on top
   <Grid container spacing={3}>
     stats.map(stat=> (
       <Grid item xs={12} sm={4}>
         <Paper className={classes.paper}>{stat.category}: {stat.value}</Paper>
       </Grid>
     ))
   </Grid>
   //more code below
)}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...