简体   繁体   English

是否有更好的方法来避免这种多维数据中的重复?

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

here's my data from weather API.这是我来自天气 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. ps 我不应该为我用来实现目标的包更改数据结构和变量名称。

tried for loops, and map method to iterate this data.尝试使用 for 循环和 map 方法来迭代此数据。 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}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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