I have this array
[[11,12,13,14],[21,22,23,24],[31,32,33,34],[41],[43],[51]]
,
expected output like this: 11-14,21-24,31-34,41,43,51
You can use Array.prototype.reduce():
const data = [[11,12,13,14],[21,22,23,24],[31,32,33,34],[41],[43],[51]]
const result = data.reduce(
(a, arr) => [...a, arr.length >= 2 ? `${arr[0]}-${arr[arr.length - 1]}` : arr[0]],
[]
)
console.log(result)
array[0]. Otherwise, concatenate the first and last elements with-between them.