const cars = [ { model: "Honda Civic", coloursByPopularity: ["black", "silver"], speedStats: { topSpeed: 140, zeroToSixty: 8.5 } }, { model: "Tesla Model 3", coloursByPopularity: ["red", "white"], speedStats: { topSpeed: 150, zeroToSixty: 3.2 } } ]; export default cars; ------- App Page -------------- import React from "react"; import ReactDOM from "react-dom"; import cars from "./practice"; const [honda, tesla] = cars; const {speedStats: { topSpeed: hondaTopSpeed }} = honda; const {speedStats: { topSpeed: teslaTopSpeed }} = tesla; const {coloursByPopularity: [hondaTopColour]} = honda; // access to item object and set variable name to the item of the nested object. const {coloursByPopularity: [teslaTopColour]} = tesla; ReactDOM.render( <table> <tr> <th>Brand</th> <th>Top Speed</th> <th>Top Colour</th> </tr> <tr> <td>{tesla.model}</td> <td>{teslaTopSpeed}</td> <td>{teslaTopColour}</td> </tr> <tr> <td>{honda.model}</td> <td>{hondaTopSpeed}</td> <td>{hondaTopColour}</td> </tr> </table>, document.getElementById("root") );
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter