Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!