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

270
Views
React how to setState array with another array?

I would like to set an array just with the values from another array. Should I use push for this?

well, in this case, I'm trying to get the selectedOptions array and set the selectedTags array just with selectedOptions value

but just push 1 value from the selectedOptions array!

So, someone please can spare a hint on how to map an array and get the values from it?

handleChange = (selectedOptions) => {
    this.setState({ selectedOptions });
      let options = [];

    selectedOptions.map((v) => options.push(v.value));
            this.setState((state) => ({
                selectedTags: options

            }));

        

  }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Mapping over an array

You can use the Array.map function:

const data = [
{id: 1, value: '1'},
{id: 2, value: '2'},
{id: 3, value: '3'},
{id: 4, value: '4'},
{id: 5, value: '5'},
{id: 6, value: '6'},
{id: 7, value: '7'},
]

const onlyValues = data.map(d => d.value)
console.log(onlyValues)

Adding to an array via spread

const data = [1,2,3,4,5,6,7,8,9,10]
const toBeAdded = 11

const newData = [...data, toBeAdded]
console.log(newData)

const toBeAddedArray = [11,12,13,14,15,16]
const newDataArray = [...data, ...toBeAddedArray]
console.log(newDataArray)

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!