I
I
Im p3l2022-01-25 16:12:00
JavaScript
Im p3l, 2022-01-25 16:12:00

How to check for the presence in the object and draw?

it is not possible to draw the necessary data. Such a variable currentCarBrand comes to me. And there is an object, depending on the value of the currentCarBrand variable, it is necessary to draw the necessary data

const entities = {
    opel: [
      {
        label: 'astra',
        icon: '',
      },
    ],
    mercedes: [
      {
        label: 'GLA',
        icon: <gla />,
      },
      {
        label: 'GLE',
        icon: <gle />,
      },
    ],
    bmw: [
      {
        label: 'x1',
        icon: <bmwx1 />,
      },
      {
        label: 'x2',
        icon: <bmwx2 />,
      },
      {
        label: 'x3',
        icon: <bmwx3 />,
      },
      {
        label: 'x4',
        icon: <bmwx4 />,
      },
      {
        label: 'x5',
        icon: <bmwx5 />,
      },
      {
        label: 'x6',
        icon: <bmwx6 />,
      },
    ],
    audi: [
      {
        label: 'q1',
        icon: <audiq1 />,
      },
      {
        label: 'q2',
        icon: <audiq2 />,
      },
      {
        label: 'q3',
        icon: <audiq3 />,
      },
      {
        label: 'q4',
        icon: <audiq4 />,
      },
      {
        label: 'q5',
        icon: <audiq5 />,
      },
    ],
  }


I tried to draw using such a function, but I do not fully understand how to do it

const renderCarGroup = () => entities.hasOwnProperty(currentCarBrand).map((item) => (
    <ListItem>
        <Car>
          {item.icon}
          {item.label}
        </Car>
    </ListItem>
))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2022-01-25
@LoranDeMarcus

(entities[currentBrand] ?? []).map(n => (
  <ListItem>
    {n.icon}
    {n.label}
  </ListItem>
))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question