split array of first and last names into variables of first and last name in JavaScript

I have an array element in a web template(the size of the array varies per page) the array has the following format: [‘John gray’,’Matt jones’, ‘Frank white’]. I would like to split the array irrespective of its size into two arrays: one containing the first names: [‘John’,’Matt’,’Frank’] and one containing the last names: [‘gray’,’jones’,’white’]. I know that for strings you can use a split() method but have not found much on splitting arrays in the manner. any ideas?

Answer

You will have to iterate over the array and for each name in the array call the split and save the parts to two different arrays.

let names = ['John gray','Matt jones', 'Frank white'];
let firstNames = [];
let lastNames = [];
names.forEach(name => {
    let splitted = name.split(" ");
    firstNames.push(splitted[0]);
    lastNames.push(splitted[1]);
});