To combine two arrays into one single dimensional array, you can use the spread operator.
let arr1=["apple","banana"];
let arr2=["cherry","date"];
mergedArr = [...arr1,...arr2]
Output:
mergedArr = ["apple", "banana", "cherry", "date"];
The spread operator in javascript spreads the contents of arrays and objects. So,
const numbers = [1,2,3,4];
...numbers // outputs 1,2,3,4 (without wrapper array)
You can then utilize these spreaded elements in various ways,
As Arguments in a Function
function add(a,b,c,d){
return a+b+c+d;
}
const values = [1,2,3,4];
const sumResult = add(...values); // results in -> add(1,2,3,4);
To Create Another Array
const nums = [1,2,3,4];
const newArr = [...nums, 5,6,7,8]; // -> [1,2,3,4,5,6,7,8];
You can even create an object
const nums = [1,2,3,4];
const newObj = {...nums};
/*
Output
{
"1":1,
"2":2,
"3":3,
"4":4
}
*/
And many more possibilities with the spread operator!