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

238
Views
How to merge arrays that derive from the same variable

I have the following line of code which returns me one or more arrays depending on the checkbox that is clicked.

selected.forEach(langsel => {
                let filtered = person.filter(pers => pers.language == langsel);
            })

selected and I do not report the other variables for simplicity in reading.

I have a list of checkboxes where each refers to a particular language. So every time a checkbox is clicked I want the people who speak that language to be returned to me; this must be returned in an array of objects.

catsel refers to the checkbox that is selected (being a checkbox, more than one can also be selected). So for each language that is selected it returns me the array of objects in the filtered variable.

For example, if I select the English language through the checkbox, I get:

[{id: "2", name: "Tomas Addreh", language: "English"},{id: "6", name: "Mark Addreh", language: "English"}];

if together with the checkbox selected previously, therefore English, I also select the checkbox relating to the Spanish language, filtered returns me:

[{id: "2", name: "Tomas Addreh", language: "English"},{id: "6", name: "Mark Addreh", language: "English"}];

[{id: "15", name: "Alex Atres", language: "Spanish"}, {id: "1", name: "Mark Sertoj", language: "Spanish"}, id: "12", name: "Martha Forrest", language: "Spanish"];

filtered in the latter case returns me two separate arrays.

I want them to be merged into an array.

Can anyone kindly help me?

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

0

I concat the array by using spread operator.

let people = [];
selected.forEach(langsel => {
    let filteredPerson = person.filter(pers => pers.language == langsel);
    people = [...people, ...filteredPerson];
})
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!