0

I have an array,

const arr = [
  { age: 1, name: 'Raphael' },
  { age: 3, name: 'Inyang' },
  { age: 6, name: 'Ifiok' },
  { age: 8, name: 'Ekpedeme' }
];

I need ages above 5 to have an opacity of 0.5, while the rest will have an opacity of 1

function changeOpacityOfArray(letter) {
        if (arr.age >= 5 ) {
      letter.style.opacity= '0.5';
    }
  
}

changeOpacityOfArray(arr);

The above doesn't work in JSX,

it says cannot style an undefined element

Then in the HTML body, note the opacity in the styling

<ul style={{listStyleType: 'none'}}>
    {arr.map(function(item){
         return (<li><div style={{display: 'flex', justifyContent: 'flex-start', width: 'auto', fontSize: 'calc(4px + 2vmin)', opacity: 1, justifyContent: 'flex-start' }}><p>{item.name}: {item.age}</p></div></li>)
      }
       )}

       </ul>  
3
  • whats letter aka messages Commented Oct 12, 2020 at 8:38
  • Voting to close because the problem is caused by a typo. The property name is opacity not Opacity. JavaScript is case-sensitive. Commented Oct 12, 2020 at 8:43
  • please, don't close, I've changed the typos Commented Oct 12, 2020 at 8:57

2 Answers 2

3

Why don't you move the age check into the style prop?

{arr.map(function (item) {
      return (
        <li>
          <div
            style={{
              display: "flex",
              justifyContent: "flex-start",
              width: "auto",
              fontSize: "calc(4px + 2vmin)",
              opacity: item.age > 5 ? 0.5 : 1,
              justifyContent: "flex-start",
            }}
          >
            <p>
              {item.name}: {item.age}
            </p>
          </div>
        </li>
      );
    })}

Also you have a typo in the opacity prop, you have to write it in lowercase

Sign up to request clarification or add additional context in comments.

3 Comments

wow, i never thought of this, i'll try and get back to you
it was actually a typo
But i need only the part of the array that above 5, to be given the opacity, not the whole array
1

When styling the div you can check the item's age and set the opacity accordingly.

const arr = [
  { age: 1, name: 'Raphael' },
  { age: 3, name: 'Inyang' },
  { age: 6, name: 'Ifiok' },
  { age: 8, name: 'Ekpedeme' },
];

<ul style={{ listStyleType: 'none' }}>
  {arr.map(function (item) {
    return (
      <li>
        <div
          style={{
            display: 'flex',
            justifyContent: 'flex-start',
            width: 'auto',
            fontSize: 'calc(4px + 2vmin)',
            opacity: item.age > 5 ? 0.5 : 1,
            justifyContent: 'flex-start',
          }}>
          <p>
            {item.name}: {item.age}
          </p>
        </div>
      </li>
    );
  })}
</ul>;

3 Comments

for this, the whole list will be affected, not only part of the list affected by the condition
you are just defaulting to opacity 1 for the other items in the list and just setting 0.5 for the ones with age > 5.
it worked, so how will I make more research on it, like what is the name of the process

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.