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

222
Views
How to use map function for two arrays and concatenate

I have two arrays.

const test = ["SME","ONE", "TWO"]
const test2 = ["RED"]  // can have multiple elements

I am trying to map over this and return an object like this :

[{SME: "SME", isValid: Y}, {ONE: "ONE", isValid: Y}, {"TWO": isValid: Y}, {"RED": "N"}]

How can I create such Data structure using map ?

I tried :

test.map((item) => ({
   item,
    isValid: Y
})

test1.map((item) => ({
   item,
isValid: N
})

[...test, ...test2]

This way it works , but any other solution for this ? We can not combine these two arrays at start

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

0

Your solution is not bad, you could also use flatMap:

const test = ["SME", "ONE", "TWO"];
const test2 = ["RED"];

const result = [test, test2].flatMap(arr => {
  return arr.map(item => {
    return {
      item,
      isValid: arr === test,
    }
  })
})

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

As you suggestet:

[...test, ...test2]

Or using concat

test.concat(test2)

Or without creating a new array, just pushing into test

Array.prototype.push.apply(test, test2)
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!