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

218
Views
How to join two arrays in a map function?

What I want is to join my arrays that I get from my map function.

I tried to do with reduce :

const onFinish = (values) => {
    values.fields.map((el, idx) => {           
      
      console.log({ ...el, index: idx }); // this `console.log` prints me one array after the other
    }).reduce((acc, x) => {console.log(acc,x)}); // I tried with `reduce` but it prints me null

Also tried to do with useState:

 const onFinish = (values) => {
    values.fields.map((el, idx) => {           
      if (myArray === null){
        setMyArray(() => {
          return { ...el, index: idx };
        });
      }
      else {
        setMyArray(() => {
           return {...myArray,...el, index: idx };
        });
      }
    });
      console.log(myArray) // at my first call to this const onFinish myArray is printed as [] and at the second it prints with the last object only
   };

Someone knows how to get these objects joined/merged ?

Sample data the way that it's print: enter image description here

How I want to be: enter image description here

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Altghough it is still not very clear from the screenshots (always prefer to use text and not images for data/code), it looks like you want

const onFinish = (values) => {
  const updatedValues = values.fields.map((field, index) => ({...field, index}));
  console.log( updatedValues );
}
about 4 years ago · Juan Pablo Isaza Report

0

Does this help?

  if (myArray === null){
    setMyArray(() => {
      return [{ ...el, index: idx }];
    });
  }
  else {
    setMyArray(() => {
       return [...myArray, {...el, index: idx }];
    });
  }
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!