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

272
Views
How to merge elements of two string arrays in javascript?

Let's say we have two JavaScript arrays like this:

var firstNameArray = ["Jack", "Indiana", "James"];

var lastNameArray = ["Sparrow", "Jones", "Bond"];

I want the output to be:

var result = ["Jack Sparrow","Indiana Jones","James Bond"];

Note: firstNameArray and lastNameArray can have dynamic values.

How do I merge elements of two arrays in JavaScript to get this result?

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

0

Using traditional for loop

var firstNameArray = ["Jack", "Indiana", "James"];
var lastNameArray = ["Sparrow", "Jones", "Bond"];
let merged = [];
for (let i = 0; i < firstNameArray.length; i++) {
  merged.push(`${firstNameArray[i]} ${lastNameArray[i]}`)
}
console.log(merged);

Using reduce method

var firstNameArray = ["Jack", "Indiana", "James"];
var lastNameArray = ["Sparrow", "Jones", "Bond"];
const merged = firstNameArray.reduce((arr, item, index) => {
  arr.push(`${item} ${lastNameArray[index]}`);
  return arr;
}, []);
console.log(merged)

Using map

var firstNameArray = ["Jack", "Indiana", "James"];
var lastNameArray = ["Sparrow", "Jones", "Bond"];
let merged = firstNameArray.map((e,index) => {
  return `${e} ${lastNameArray[index]}`;
});
console.log(merged);

about 4 years ago · Juan Pablo Isaza Report

0

var result = firstNameArray.map((it,index)=>it+' '+lastNameArray[index]);

about 4 years ago · Juan Pablo Isaza Report

0

You can iterate any array (assuming both the arrays will contain equal number of elements) using map and build the new set with concatenation both the array elements based on index.

Demo :

const firstNameArray = ["Jack", "Indiana", "James"];

const lastNameArray = ["Sparrow", "Jones", "Bond"];

const finalArray = firstNameArray.map((item, index) => {
    return `${item} ${lastNameArray[index]}`
});

console.log(finalArray);

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!