简体   繁体   English

获取 React 状态(对象的对象(内部示例))并将对象数组获取到 map() 以进行组件渲染

[英]Taking React State (object of objects(example inside)) and getting an array of objects to map() for component rendering

Given the below state of my App how can I convert it to an array of objects that I can map() to iterate over and render child components?鉴于我的应用程序的以下状态,我如何将其转换为我可以 map() 迭代并呈现子组件的对象数组?

Project focus is to provide a front end UI for a greenhouse climate control system.项目重点是为温室气候控制系统提供前端 UI。 Reactjs frontend. Reactjs 前端。

    this.state = {
      greenhousesById: {
        0: {
          name: "Mercury",
          temperature: 72,
          humidity: 60,
          fans: [0],
          heater: [0]
        },
        1: {
          name: "Venus",
          temperature: 72,
          humidity: 60,
          fans: [1],
          heater: [1]
        },
        2: {
          name: "Earth",
          temperature: 72,
          humidity: 60,
          fans: [2],
          heater: [2]
        }
      },

      allGreenhouses: [0, 1, 2]
    };
  }

Plan is to use allGreenhouses as id's then pull those id'd objects from state to create a new array of objects so that I can render then as Greenhouse component (using the id as key in the child components).计划是使用 allGreenhouses 作为 id,然后从 state 中提取那些 id 的对象以创建一个新的对象数组,以便我可以将其渲染为 Greenhouse 组件(使用 id 作为子组件中的键)。

Expected result would be [{obj1Data},{obj2Data},{obj3Data}].预期结果为 [{obj1Data},{obj2Data},{obj3Data}]。 I hope this is concise enough, this is my first question.我希望这足够简洁,这是我的第一个问题。

You could use the Object#values() method for this.您可以为此使用Object#values()方法。 In your render method, access your state and pass greenhousesById to Object.values().在您的渲染方法中,访问您的状态并将greenhousesById ById 传递给 Object.values()。 This returns the values of greenhousesById as an array which you can then call map() on to render each list item:这将作为一个数组返回greenhousesById的值,然后您可以调用map()来呈现每个列表项:

 // Your component state var state = { greenhousesById: { 0: { name: "Mercury", temperature: 72, humidity: 60, fans: [0], heater: [0] }, 1: { name: "Venus", temperature: 72, humidity: 60, fans: [1], heater: [1] }, 2: { name: "Earth", temperature: 72, humidity: 60, fans: [2], heater: [2] } }, allGreenhouses: [0, 1, 2] }; // In your render method, access your state and pass greenhousesById // to Object.values(). Call map() to render each value as list item. var result = Object.values(state.greenhousesById); console.log(result);

Hope this helps!希望这可以帮助!

You could be use map() for your allGreenhouses你可以为你的allGreenhouses使用map()

DEMO演示

 var state = { greenhousesById: { 0: { name: "Mercury", temperature: 72, humidity: 60, fans: [0], heater: [0] }, 1: { name: "Venus", temperature: 72, humidity: 60, fans: [1], heater: [1] }, 2: { name: "Earth", temperature: 72, humidity: 60, fans: [2], heater: [2] } }, allGreenhouses: [0, 1, 2] }; console.log(state.allGreenhouses.map(v=>state.greenhousesById[v]));

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

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