I have an array of arrays. I want to merge the first item from each array within the array. How could I go about that?
Here's my array:
var array = [["plums", "peaches", "pineapples"], ["carrots", "corn", "green beans"], ["chocolate", "ice cream"]];
I want the output to be:
var newArray = ["plums", "carrots", "chocolate"];
This is what I tried:
<!doctype html>
<html>
<body>
<script>
var array = [["plums", "peaches", "pineapples"], ["carrots", "corn", "green beans"], ["chocolate", "ice cream"]];
var arrLength = array.length;
var newArray = [];
for (var i = 0; i < arrLength; i++) {
newArray = [].concat.apply([], array[i][0]);
};
console.log("new merged array:", newArray);
</script>
</body>
</html>
However, in the console, I get this error:
CreateListFromArrayLike called on non-object.
How can I accomplish this?