-1

I need to compare these 2 arrays to get hex.

const colours = [
 {value: 2, hex: "#9EFF9C"},
 {value: 4, hex: "#31FF00"},
 {value: 6, hex: "#FFFF00"},
];

const users = [
{name: 'John', value: 2},
{name: 'Adam', value: 4},
{name: 'James', value: 6},
];

if(colours.value === users.value) {
  // get colours.hex ... 
}

I need to pass the hex param in style for example

<h1 style={`${hexColor}`}>{user}</h1>

2 Answers2

0

Here is an example:

const colours = [{
    value: 2,
    hex: "#9EFF9C"
  },
  {
    value: 4,
    hex: "#31FF00"
  },
  {
    value: 6,
    hex: "#FFFF00"
  },
];

const users = [{
    name: 'John',
    value: 2
  },
  {
    name: 'Adam',
    value: 4
  },
  {
    name: 'James',
    value: 6
  },
];

users.map(user => {
  const hexColor = colours.find(color => color.value === user.value);

  return <h1 style={`color: ${hexColor}`}>{user.name}</h1>
})
Joris
  • 2,416
  • 13
  • 18
0

Iterate like this:

{
    users.map(user =>
          <h1 style={{color: `${colours.find(f => f.value === user.value).hex}`}}>{user.name}</h1>
    )
}

Full source:

const colours = [
    {value: 2, hex: "#9EFF9C"},
    {value: 4, hex: "#31FF00"},
    {value: 6, hex: "#FFFF00"},
];
   
const users = [
   {name: 'John', value: 2},
   {name: 'Adam', value: 4},
   {name: 'James', value: 6},
];
   
  return (
    <div className="App">
      {
        users.map(user =>
          <h1 style={{color: `${colours.find(f => f.value === user.value).hex}`}}>{user.name}</h1>
        )
      }
    </div>
  );

Demo at CodeSandbox.

Result: enter image description here

Zunayed Shahriar
  • 2,557
  • 2
  • 12
  • 15