简体   繁体   中英

Would there be better way to avoid duplication in this multi-dimentional data?

here's my data from weather API. I'm trying not to repeat this data, and am not sure if it's even possible to iterate in this kinda data structure. if anyone knows better way, please let me know.

ps I shouldn't change data structure and variable name whatsoever for packages I use to achieve a goal.

tried for loops, and map method to iterate this data. However there wasn't an error or data didn't show up on console.

const item = this.props.hourly

        let data = [
            { x: item[0].time, y: item[0].apparentTemperature },
            { x: item[1].time, y: item[1].apparentTemperature },
            { x: item[2].time, y: item[2].apparentTemperature },
            { x: item[3].time, y: item[3].apparentTemperature },
            { x: item[4].time, y: item[4].apparentTemperature },
        ]

I look forward to better solution to avoid repeat in code.

let data = item.slice(0, 5).map(({time: x, apparentTemperature: y}) => ({x, y}));

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