Array.concat()
-
Hey!
How do I use the
concat()
function?const var tagArray = [["Pad"], ["Chord", "String"], ["Pluck"]]
Trying to make ^this^ turn into this:
["Pad", "Chord", "String", "Pluck"]
Thanks!
-
@Casmat Array concat is used for combining two arrays, so that won't do what you want.
You need to flatten the array. I don't think there is a clean way to do it in HISE (with JS we'd usually use the spread or apply commands). So here is a dirty way using a recursive function.
const var tagArray = [["Pad"], ["Chord", "String"], ["Pluck"]] function flattenArray(arr) { var result = []; for (i = 0; i < arr.length; i++) { if (Array.isArray(arr[i])) { var flattenedSubArray = flattenArray(arr[i]); for (j = 0; j < flattenedSubArray.length; j++) result.push(flattenedSubArray[j]); } else { result.push(arr[i]); } } return result; } const newArray = flattenArray(tagArray); Console.print(trace(newArray));
You might be able to get it a bit neater using the map function but I haven't explored it for this purpose.
-
@d-healey ahh, makes sense! Thanks!
-
@Casmat you can do like this
const var tagArray = [["Pad"], ["Chord", "String"], ["Pluck"]]; const result = []; for (i = 0; i < tagArray.length; i++) result.concat(tagArray[i]); Console.print(trace(result)); Interface: [ "Pad", "Chord", "String", "Pluck" ]
-
@ulrik Oh that puts my over-engineered method to shame :D
-