Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Steven Gore
seal-mask
.a{fill-rule:evenodd;}techdegree
Steven Gore
Full Stack JavaScript Techdegree Student 9,279 Points

Please explain how [...planets] can be used?

I originally had the code like this and it worked. But I noticed in the final project, [...planet] was used. I don't understand how this works.

My original working code without [...planet]

const Container = (props) => { return ( <div class="container"> {props.planets.map((p) => ( <Planet url={p.url} name={p.name} desc={p.desc} moons={p.moons} diameter={p.diameter} key={p.id} /> ))} </div> ); };

1 Answer

Robert Bourton
Robert Bourton
6,179 Points

The Spread Operator There are many ways you can use the spread operator.

const numbers = [ 1, 2, 3 ]

function print( one, two, three) {
   console.log(one)
   console.log(two)
   console.log(three)
}

print( ...numbers )
function array( ...args ) {
   for (let i = 0; i < args.length; i++)
        console.log( args[i] )
}

array( 1, 2, 3)
const array1 = [ 1, 2, 3 ]
const array2 = [4, 5, 6 ]
const array3 = [ ...array1, ...array2 ]
let obj1 = { one: 1, two: 2 }
let obj2 = { three: 3, four: 4 }

let copy = { ...obj1 }
let merge = { ...obj1, ...obj2 }

I will need to see your whole code to help you out with this. Looks like your trying to build a HTML String.

Robert Bourton
Robert Bourton
6,179 Points

I don't use semicolons in my JavaScript code, since they are optional.

Steven Parker
Steven Parker
216,732 Points

Semicolons after statements may be technically optional, but they are most definitely a "best practice".
You might want to reconsider using them!   :speak_no_evil: