Henok Tesfaye
Henok Tesfaye

Reputation: 9560

How to apply style property for a specific entity in Array.map?

const imageHtml = imageDescription.map((article, index) => {
            return (
                <div style={{ backgroundImage: "url(/images/Large/" + article.imagePath + ".jpg)" }}>

                </div>
            )
        })

I am doing a slider thing but i faced a problem, how to apply left property for a specific article? For example applying left = 20px for only index 1?

Upvotes: 0

Views: 55

Answers (1)

Meir
Meir

Reputation: 14375

You can either give left=0 for the rest:

<div style={{backgroundImage: ..., left: (index === 1 ? 20 : 0)}} />

or, if you prefer to not include it at all:

<div style={Object.assign({backgroundImage: ...}, (index === 1 ? {left: 20} : {}))} />

Upvotes: 1

Related Questions