Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
Map through the data, if matching ID found in two arrays then render second array, otherwise render generic data

a bit stuck with array methods. I'm given two arrays

Steps array:

const steps = [
  {
    id: 1,
    name: "Step 1",
    description: "Description 1"
  },
  {
    id: 2,
    name: "Step 2",
    description: "Description 2"
  },
  {
    id: 3,
    name: "Step 3",
    description: "Description 3"
  }
];

Val array:

const val = [
  {
    id: 1,
    a: 2,
    b: 1,
    c: 5
  },
  {
    id: 2,
    a: 2,
    b: 2,
    c: 1
  },
  {
    id: 5,
    a: 0,
    b: 0,
    c: 0
  }
];

What I have here is 3 objects in the Steps array and three objects in the val array. I would like to render all steps and then render val array only if ID's are matching, if not then use 0's instead (so with the example the last step would get val.a: 0, val.b: 0, val.c: 0)

Here's what I got:

 <div className="App">
      {steps.map((step) => (
        <div style={{ marginBottom: "20px" }}>
          <div className="step-name">{step.name}</div>
          <div className="step-name">{step.description}</div>
          {val
            .filter((value) => value.id === step.id)
            .map((value) => (
              <>
                <div>value a: {value.a}</div>
                <div>value b: {value.b}</div>
                <div>value c: {value.c}</div>
              </>
            ))}
        </div>
      ))}
    </div>

With the current piece it only renders values if the ID's are matching, I am missing the part where it should render 0's for the steps that matching id's are not found.

Also codesandbox link: Codesandbox

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issue with your code is that when no match is found the filtered array is an empty array with no elements to map on.

you can do something like creating a function which returns the filtered array if found or returns an array of object with all 0's for a,b,c. This logic can be implemented inside the render as well but this way much cleaner and less messy

const filteredArr = (step) => {
  const arr = val.filter((value) => value.id === step.id)
  return arr.length>0? arr : [{id: step.id, a:0,b:0,c:0}]
}

then you can call this inside your render

  return (
    <div className="App">
      {steps.map((step) => (
        <div style={{ marginBottom: "20px" }}>
          <div className="step-name">{step.name}</div>
          <div className="step-name">{step.description}</div>
          {filteredArr(step)
            .map((value) => (
              <>
                <div>value a : {value.a}</div>
                <div>value b : {value.b}</div>
                <div>value c : {value.c}</div>
              </>
            ))}
        </div>
      ))}
    </div>
  );

sandbox

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda