• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 79
  • Last Modified:

Combine 3 arrays into 1

Hi..
I have 3 arrays

var lastname = ['Smith','Jones'];
var firstname = ['John','Bob'];
var middlename = ['Fred','Tom'];

How can I combine these into 1 array of 'Names'

like  'John Fred Smith' , Bob Tom Jones'

How would you do this using LO-DASH

thanks!
0
JElster
Asked:
JElster
  • 2
1 Solution
 
Alexandre SimõesManager / Technology SpecialistCommented:
You can use plain javascript:
var lastname = ['Smith','Jones'];
var firstname = ['John','Bob'];
var middlename = ['Fred','Tom'];

var newArray = lastname.concat(firstname, middlename);

Open in new window

http://www.w3schools.com/jsref/jsref_concat_array.asp
0
 
JElsterAuthor Commented:
I need the new array to look like

 ' John Fred Smith' , ' Bob Tom Jones'    - 2 values
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Ok, this will do the job:
var lastname = ['Smith','Jones'];
var firstname = ['John','Bob'];
var middlename = ['Fred','Tom'];

var result = [];
for(var i=0; i<firstname.length; i++){
    var name = [];
    name.push(firstname[i]);
    name.push(middlename[i]);
    name.push(lastname[i]);
    
    result.push(name.join(' '));
}

console.log(result);

Open in new window

Just make sure all the arrays have the same amount of items.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now