Is there a function that lets me concat several arrays, with delimiters between them (the delimiters are also arrays), similarly to how join
works but not restricted to strings?
The function can be standard JS or part of a major library such as lodash
(which is why it's referenced in the tags).
Here is an example of usage:
let numbers = [[1], [2], [3]];
let result = _.joinArrays(numbers, [0]);
console.log(result);
//printed: [1, 0, 2, 0, 3]
This is analogous to:
let strings = ["a", "b", "c"];
let result = strings.join(",");
console.log(result);
//printed: "a,b,c";
However, join
can't be used because it turns values into strings, which I don't want to happen.
But it works for any type.
You could simply use array.reduce
to concat the arrays, and push what ever you want to use as your delimiter.
let numbers = [[1], [2], [3]];
let n = numbers.reduce((a, b) => a.concat(0, b))
console.log(n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With