0
  socialLinks: {
    instagram: 'https://twitter.com/xyz',
    facebook: 'https://facebook.com/xyz',
    linkedin: '#',
  },

how to create a loop so that result should come like this -

<div>
   <a href="https://instagram.com/xyz">
      <image src="instagram.png" />
   </a>
   <a href="https://facebook.com/xyz">
      <image src="facebook.png" />
   </a>
<div>

Note: If there is no link of linkedin then no link and image will come

2 Answers 2

1

You can take advantage of Object.entries:

<div>
    {
        Object.entries(socialLinks).filter(([k,v]) => v.startsWith("https:")).map(([k,v]) => 
            <a key={v} href={v}>
                <image src={`${k}.png`} />
            </a>
        )
    }
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

This fixes my requirement. Thanks Sir. Just one warning: 'k' is declared but its value is never read. Any thing missing !!
@NishaKapoor I guess it's because of this part: filter(([k,v]) => v.startsWith("https:")) you can use filter(kv => kv[1].startsWith("https:")) instead
0
const SocialMedia = () => {
    let socialLink = {
        instagram: 'https://twitter.com/xyz',
        facebook: 'https://facebook.com/xyz',
        twitter: 'https://twitter.com/xyz'
    }
    return (
        <div>
            {
                Object.keys(socialLink).map((key, index) => {
                    return (
                        <a href={socialLink[key]} key={index}>
                            <image src={key+'.png'} />
                        </a>
                    )
                })
            }
        </div>
    )
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.